You are here: Re: [PHP] String format problem « PHP « IT news, forums, messages
Re: [PHP] String format problem

Posted by tg-php on 10/20/56 11:25

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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация