Jump to content

php operators and variables


Guest Cashster09

Recommended Posts

Guest Cashster09

I am going thru some tutorials and came across this and wondered why? I am a beginner with this stuff...thanks in advance

 

on the echo statements they are surrounded by "". HOw come the variables are surrounded by quotes and a period?

 

Why not just "Perform addition: 2 + 4 = $addition";

 

$addition = 2 + 4;

$subtraction = 6 - 2;

$multiplication = 5 * 3;

$division = 15 / 3;

$modulus = 5 % 2;

echo "Perform addition: 2 + 4 = ".$addition."<br />";

echo "Perform subtraction: 6 - 2 = ".$subtraction."

";

echo "Perform multiplication: 5 * 3 = ".$multiplication."

";

echo "Perform division: 15 / 3 = ".$division."

";

echo "Perform modulus: 5 % 2 = " . $modulus

. ". Modulus is the remainder after the division operation has been performed.

In this case it was 5 / 2, which has a remainder of 1.";

 

thanks in advance,

Rich

Link to comment
Share on other sites

The period is a concatenation operator. It separates different parts of the string to be echo'd out.

 

echo "This part " . $this_variable . " and another part. 
";

 

Notice also that the same output can be done using single quotes:

 

echo 'This part ' . $this_variable . ' and another part. 
';

 

Or Double quotes as you suggest:

 

echo "This part  $this_variable  and another part. 
";

 

Test script:

$this_variable = "This Variable";
echo "This part " . $this_variable . " and another part. 
";
echo 'This part ' . $this_variable . ' and another part. 
';
echo "This part  $this_variable  and another part. 
";
?>

The biggest difference is that the single quote stuff does not get 'parsed' and it will be faster on the Server. Not a big deal on a small script, but it can bog down on a larger script.

Link to comment
Share on other sites

Guest Cashster09

Ok, now i see!

I was looking at all the quotes differently....it looked like there were quotes around the variables....

 

It really is

echo "Perform addition: 2 + 4 = " .$addition. "

";

 

So the concatenation can have spaces or not?

echo "Perform addition: 2 + 4 = " . $addition . "

"; Which is easier to read and decipher than the one below

echo "Perform addition: 2 + 4 = ".$addition."

";

 

both these are correct, then?

Edited by Cashster09
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...