Miscellaneous Things You Should Know
Not everything fits into a neat little category, nor does everything in PHP belong in this reference book. However, sometimes they deserve a quick note and a little attention and shall be included here.
PHP Code
For portability and compatibility, always use the long form.
Long form:
<?php expr ?>
Short form:
<? expr ?>
Short form equivalent of <? echo expr ?> Note: No closing semicolon (;) is required.
<?= expr ?>
Semicolon ( ; )
All statements must end in a semicolon ( ; )! Otherwise, errors will be generated. If the error doesn't make sense, you probably are missing a semicolon somewhere!
Quotations
' ' (single quotes) – Content inside single quotes is evaluated literally. Therefore, $string actually means: (dollar sign)string, and does not represent the variable's value.
Example:
$string = 'Single Quotes';
echo '$string';
" " (double quotes) – Variables inside double quotes are evaluated for their values.
Example:
$string = 'Double Quotes';
echo "$string";
Double Quotes
Backslash (Escape Character)
Escapes characters that should be evaluated literally when inside double quotations.
Example:
$string = 'Double Quotes';
echo "\$string is set as $string";
$string is set as Double Quotes
Special Characters
backslash ( \ )
question mark ( ? )
single ( ' ) quotes
double ( " ) quotes
dollar sign ( $ )
Example:
$string = 'Hello World!';
echo "The variable \$string contains \' $string \' \" \\";
The variable $string contains \' Hello World! \' " \
echo 'The variable \$string contains \' $string \' \" \\';
The variable \$string contains ' $string ' \" \
Comments
Single line, for everything to the right of the double forward slashes:
// This is a comment
Multiple lines, opening and closing tags:
/* */
/* This is
a comment */
PHP Reference: Beginner to Intermediate PHP5
Formatting Characters
\n – New line
\r – Carriage return
\t – Tab
\b – Backspace
define(name, value [, $boolean])
name – $string
value – $scalar
$boolean – [optional] default: FALSE, case-sensitive
Define a constant, a set value that is assigned globally, making it available to functions and classes without passing them directly as an argument.
Examples:
define('HELLO', 'Hello World!');
echo HELLO;
Hello World!
define('GREETINGS', 'Hello World!', TRUE);
echo GREETINGS;
echo greetings;
Hello World!Hello World!
Functions
function functionname([arguments]) { }
Functions can be placed anywhere in a page and will be available even if called above the actual function being created. The exception to this rule is if the function is only defined as part of a conditional statement, and is not available to be called until that conditional statement has been evaluated.
Examples:
hello();
// Above the conditional statement, this will cause an error
if (0==0){
function hello(){
echo 'Hello!';
}
}
Fatal error: Call to undefined function hello()
if (0==0){
function hello(){
echo 'Hello ';
}
}
hello();
there();
function there(){
echo 'there';
}
Hello there
Functions can have no arguments (as above), arguments passed to them, or default arguments with passed arguments as optional. The argument names are used within that function as variable names.
function args($a, $b){
// Has no default values, requires two inputs echo "a = $a, b = $b";
}
args(1,2);
a = 1, b = 2
Some examples using the following function:
function args($a = 1, $b = 2){
// Has default values set $c = $a + $b;
echo "a = $a, b = $b, a+b = $c";
}
args();
a = 1, b = 2, a+b = 3
args(5,5);
a = 5, b = 5, a+b = 10
args(10);
a = 10, b = 2, a+b = 12
args($DoesNotExist,20); // Do not do this, send (NULL,20) instead
a = , b = 20, a+b = 20
Functions can also return a $variable (including an array):
function Add($one,$two){
$total = $one + $two;
return $total;
}
$a = 2;
$b = 3;
$result = Add($a,$b); // Assigns the value of $total to $result echo $result;
5
♣ ♣ ♣
NOTE , To be continue NEXT Day
No comments:
Post a Comment