|
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). For example: $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. For example: $a = 4; $b = "5"; $c = $a + $b; // $c = 9; PHP also supports boolean variables, which can be assigned either a one or a zero, or the words true or false: $a = true; $b = 1; //$a = $b $c = false; $d = 0; //$c = $d Variable Naming Rules
- A variable name must start with a letter or an underscore.
- A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
- A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
Comments:
You can include comments in your PHP scripts. Comments are ignored by the Web server, and any comments contained within the PHP code are not sent to a browser. There are three forms of comments: # Used just like it is used in PERL; comments out the remainder of the line after the # symbol. // Used just like it is in JavaScript; comments out the remainder of the line after the // symbols. /* and */— Comments out anything in between the two sets of symbols. This is the same syntax used in C to comment code. Examples of comments in PHP code: % |