Computing.Net > Forums > Web Development > Four characters in PHP

Four characters in PHP

Reply to Message Icon

Original Message
Name: rsasalm
Date: September 24, 2005 at 01:17:52 Pacific
Subject: Four characters in PHP
OS: Win XP
CPU/Ram: P4
Comment:

Hi all,

These four characters " ' \ and null in PHP makes me very confused.
Can some give me explain these with simple examples or give me poiniter to some simple tutorial which explains the difference with examples.

thanks
regards
/rsasalm


Report Offensive Message For Removal

Response Number 1
Name: Michael J (by mjdamato)
Date: September 24, 2005 at 06:54:29 Pacific
Subject: Four characters in PHP
Reply: (edit)

Hmm... Don't know of any tutorials about specific characters. But, by far the most useful site for me is php.net. I'll give you a little info though.

The quotemark characters (" and ') are used to define a string, either when defining a value or when used for comparisson.

$strVar = "This is a string"; OR
$strVar = 'This is a string';

Both uses are valid. Where some of your confusion might be coming from is the use of quotes within a string. For example what if you wanted the string to be Let's go. Well, you can't just use single quotes, as above, to define the string like this:

$strVar = 'Let's go'; //(incorrect)

The problem with this is that the second single quote mark (in let's) would be interpreted as the end of the string! The remainder of the line s go' would try to be interpreted and would result in a parsing error. There are two solutions. The first is to use double quotes to define your string:

$strVar = "Let's go";

That way the single quote is interpreted as part of the string. You could also escape the inner single quote like this:

$strVar = 'Let\'s go';

By using the \ before the single quote you are telling the interpreter to treat the next character as a literal character rather than a programming construct. For example if you needed to include the \ character in a string you would have to use two of them (\\).

As a last example, if you wanted a string to be set to He said, "Let's go." you could write it like this:

$strVar = "He said, \"Let's go\""; OR

$strVar = 'He said, "Let\'s go"';

Michael J


Report Offensive Follow Up For Removal

Response Number 2
Name: FBI Agent
Date: September 24, 2005 at 08:11:03 Pacific
Subject: Four characters in PHP
Reply: (edit)

here's some more info on that...

$str1 = "asdf asdf";
$str2 = '$str1';

echo $str2; // output will be '$str1' because using single quotes it doesnt parse the PHP code, that's why i ALWAYS use double quotes (there are some cases where you want only single quotes but this isnt one of them)

as for NULL, it's like this...

$str1 = "";
$str2 = NULL;

SAME THING so you can do it either way. have fun

FBI Agent

AIM: EliteAssassin187


Report Offensive Follow Up For Removal

Response Number 3
Name: rsasalm
Date: September 24, 2005 at 09:33:17 Pacific
Subject: Four characters in PHP
Reply: (edit)

Hi all,
Thanks a lot for explation with examples. Now it is clear to me the difference about above mentioned characters.
I have one more question, it might sound you a basic question again.

i found a piece of code and there I found two line of code
as below
Line1 :<?= $errmsg ?>
and
Line2:<center>

Regarding Line1
what does it mean by <?= and ?>
Regarding Line2
what does it mean by <?= and ?>
thanks again for help
regards
/rsasalm


Report Offensive Follow Up For Removal

Response Number 4
Name: Michael J (by mjdamato)
Date: September 24, 2005 at 10:15:13 Pacific
Subject: Four characters in PHP
Reply: (edit)

Regarding Line1
what does it mean by <?= and ?>

That is a shortcut for "printing" the value of a variable to the page without having to go into the more formal method. For any code to be processed as PHP code it must be contained within the PHP opening and closing tags ("<?php" & "?>"). Anything outside of those tags is considered regular HTML. The above method is a quick way to insert the value of a variable when you are in HTML mode. So you could write:

Hi my name is <?= $name ?>

instead of:

Hi my name is
<?php
echo $name;
?>

Regarding Line2
what does it mean by <?= and ?>

What do you mean? Line 2 doesn't have those. It is just a regular HTML center tag.

Michael J


Report Offensive Follow Up For Removal

Response Number 5
Name: rsasalm
Date: September 24, 2005 at 10:39:22 Pacific
Subject: Four characters in PHP
Reply: (edit)

Hi Michael,

Thanks for reply.

The Line2 is as follow
img src=?gim=1 width=144
What does it mean by
part of the code?

thanks again
regards
/rsasalm


Report Offensive Follow Up For Removal


Response Number 6
Name: rsasalm
Date: September 25, 2005 at 04:21:04 Pacific
Subject: Four characters in PHP
Reply: (edit)

Hi again,

Here comes an example which cofuses me regarding ' and " characters. I have a dirctory photos which lies where test.php script lies and under photos dir, there is a file 24.jpg

<?php

$images_dir = "photos";
$filename = "24.jpg";

Line1 '".$images_dir. "/tb_".$filename."';

?>
If I echo Line1 the reslut is:
".$images_dir. "/tb_".$filename." (variable values are not replaced)

If I echo in a "img src" these variable using

img=src'".$images_dir. "/tb_".$filename."'

I get 24.jpg picture in the brower.

My question is why I don't see the real values of variables when I echo in Line1those but in
when I echo Line2
these variables are replaced with real values and I can see the picture.
thanks
regards
/rsasalm


Report Offensive Follow Up For Removal

Response Number 7
Name: Michael J (by mjdamato)
Date: September 25, 2005 at 07:25:43 Pacific
Subject: Four characters in PHP
Reply: (edit)

What you wrote in line 5 makes no sense to me.

As for response 6, you are using quotes where they are not needed!

echo $images_dir . "/tb_" . $filename;

That is all that you need to write the image path to the page. By enclosing that within additional quotes it appears the PHP processor is treating it differently (treating the whole thing as a string rather than parsing it) depending upon how you are specifically using it.


Report Offensive Follow Up For Removal

Response Number 8
Name: FBI Agent
Date: September 25, 2005 at 08:13:34 Pacific
Subject: Four characters in PHP
Reply: (edit)

...ummm... you obviously werent paying attention to what i said in regards to

Line1 '".$images_dir. "/tb_".$filename."';

you have the single quotes going around the variables. back to my first post...


here's some more info on that...
$str1 = "asdf asdf";
$str2 = '$str1';

echo $str2; // output will be '$str1' because using single quotes it doesnt parse the PHP code

but you totally messed it up anyway.

here's how to do it like that. there's two ways to do it but a few forms from those ways.

Line1 "$str1/tb_$str2"; // this way is stupid

Line1 $str1."/tb_".$str2 // this is the best way

Line1 $str1."/tb_$str2" // this works too

Line1 $str1.'/tb_$str2' // single quotes!!! this would not work the way you wanted it to. but yeah, i hope you get the picture now, good luck

FBI Agent

AIM: EliteAssassin187


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Four characters in PHP

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software