Posted on Tuesday, December 15, 2009 at 1:10 PM
Variables in PHP are denoted by the "$". To assign the value "Hello World" to the variable $str, you simply call it in your code:
$str = "Hello World";
Strings must be enclosed by quotes, but they can contain variables themselves:
$a = 12;
$string = "The value of a is $a";
// $string = "The Value of a is 12";
PHP variables do not have to be declared ahead of time, nor do they require a type definition. Note that you may get a warning about using undeclared variables if you try to use them before giving them a value (depending on how you set up error reporting in php.ini).
$a = 4;
$c = $a + $b;
// $c = 4,
but a warning appears "Warning: Undefined variable..".
Warnings do not stop a script from continuing. If you forgot to add a semicolon at the end of one of the lines, then you would get a Parser error, which prohibits the script from running.
Since PHP variables are not typed, you don't have to worry about performing mathematical equations on the wrong type, as you might in C...
Posted on Tuesday, December 15, 2009 at 1:02 PM
PHP is a language that was designed to be easily embedded into HTML pages. Most PHP pages have PHP code and HTML intermixed. When a Web server reads a PHP page, it is looking for two things to let it know it should start reading the page as PHP rather than HTML, the start and end PHP tags: <?php and ?>, respectively.
If you have configured your php.ini file to accept "short tags" (which are enabled by default), then you can use the syntax <? and ?> instead. Additionally, you can configure your php.ini file so that it accepts ASP style tags, <% and %>. This feature is turned off by default, and its only real purpose seems to be to allow certain HTML editors to recognize the in-between code as something other than HTML, in which case the editor won't mangle the code by imposing its own set of HTML syntax rules upon the code.
<h1><? echo “First PHP Page.”; ?>!</h1>
The code above, when viewed via a Web server, simply prints out String enclosed in the quotes...