Re: String format problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To elaborate on Philip's response (which is correct)...

Anything in double quotes (" ") will be interpreted by PHP before figuring out what the actual value is.  Items like \n, \t, etc are therefore converted to a newline (\n) or a tab (\t) before assigning to the variable.   Variables within the double quotes are also evaluated before assigning.  So..

$firstname = "Bob";
$lastname = "Smith";
$varname = "$firstname $lastname told me to find the file in folder C:\newtext\"; 
echo $varname;

Will yield:

Bob Smith told me to find the file in folder C:
ewtext";

Actually it'll give you an error because the double quotes aren't properly terminated..  \"  is also interpreted.


Whereas..
$varname = '$firstname $lastname told me to find the file in folder C:\newtext\'; 
echo $varname;


Yields..

$firstname $lastname told me to find the file in folder C:\newtext\

In this case, you have two options to get what you want:

$varname = "$firstname $lastname told me to find the file in folder C:\\newtext\\"; 

..or..

$varname = $firstname . ' ' . $lastname told me to find the file in folder C:\newtext\'; 


I've read "never use double quotes unless you have to" because it could potentially speed up PHP a little because it won't be trying to interpret every string.  I'm not sure exactly how much savings this is going to give you so I drop this in the "whatever your style dictates" category.  I prefer double quotes because I think it visually delineates the code better and I think "$firstname $lastname" looks tidier than $firstname . ' ' . $lastname even though I do consider it slightly looser coding practices.  One's better for the human, one's better for the machine (imo) and since I'm a human..... I think.  :)


-TG

= = = Original message = = =

> hi
> i have a problem when i am formating a string
> the problem is it converts the \n in the string to a new line
> here is the code
>
> <?
> $Text = "D:\AppServ\www\intranet\admin\store\nodirectory\sub";
> $Replace = "D:\AppServ\www\intranet\admin\store";
> $with = "http://localhost/bank/admin/store";;
>
> //$doc = str_replace ($Replace, $with, $Text);
> $doc = str_replace( '\\', '/', str_replace( $Replace, $with, $Text ) );
>
> echo $doc;
>
> //i need $Doc to look like this
> http://localhost/bank/admin/store/nodirectory/sub
> //but it outputs http://localhost/bank/admin/store odirectory/sub

Replace your double quotes with single quotes... that is:

$Text = 'D:\AppServ\www\intranet\admin\store\nodirectory\sub';
$Replace = 'D:\AppServ\www\intranet\admin\store';
$with = 'http://localhost/bank/admin/store';


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux