954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Tutorial #2 Variables, echoing variables and numbers

Here's a video how is it?

Any problems?

Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Your use of quotes is incorrect. In php anything wrapped in quotes is a string. If you put a number in quotes single or double it is considered a string. If you specifically want something to be a number, do not wrap it in quotes.

<?php
$a = 5;
$b = '5';

var_dump($a); //integer
var_dump($b); //string


When working with php you have two options for quoting strings, single and double quotes.
With double quotes, php will parse any variables included within the string.

<?php
$color = "blue";
echo "The sky is $color."; //The sky is blue.


With single quotes, php will not parse any variables included within the string.

<?php
$color = 'blue';
echo 'The sky is $color.'; //The sky is $color.
echo 'The sky is '.$color.'.'; //The sky is blue.


There is a slight performance gain, at a very micro level, to using single quotes over double quotes because the interpreter does not have to look for variables. But, the trade-off is you have to use concatenation to handle variables.

Also just a pet peeve, php does not require you to close your php tags. In fact with files that only contain php it is suggested you omit the closing tag to prevent any issues with blank lines following the closing tag.

mschroeder
Work Harder
Team Colleague
666 posts since Jul 2008
Reputation Points: 279
Solved Threads: 131
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: