Difference between revisions of "PHP"

From Got Opinion Wiki
Jump to navigation Jump to search
Line 66: Line 66:
|}
|}


NOTE: Double quotes around text enables the variable name to be inserted & value is displayed. This is called '''variable interpolation''' Single quotes will not interpolate the variable name.
NOTE: Double quotes around text enables the variable name to be inserted & value is displayed. This is called '''variable interpolation'''. Single quotes will not interpolate the variable name.
 
== Arrays ==
 
A special variable that contains multiple values.
 
Simple format:
 
<code>$array_name = array('test1', 'test2', 69);</code>
 
To retrieve a value stored in an array you need the '''index'''.
 
Generally, arrays use incrementing integers that start with zero as their indices. Indices act as pointers to precisely locate a value in an array.
 
Expanding on simple format:
 
{| border="1" cellspacing="0" cellpadding="5" align="center"
! code
! comment
|-
| echo($array_name[0]);
| Outputs 'test1'
|-
| echo($array_name[1]);
| Outputs 'test2'
|-
| echo($array_name[2]);
| Outputs 69
|}

Revision as of 21:21, 16 July 2007

PHP is a server side scripting language.

Basic Syntax & Commands

PHP statements are always terminated with semicolon (;).

Single quotes are used to mark start and end of text ()

All variable names in PHP start with dollar sign ($).

PHP is a loosely typed language. Variables may contain any type of data.

Example:

$variable_name = "Semper Fidelis";

Equal sign is called the assignment operator since it assigns values to variables.

Comments begin with // or /* */

arithmetic operators enable you to add, subtract, multiply, & divide.

concatenation operator allows you to add strings of text.

Example:

$variable_name = "Hello" . " there" . " world!";

Assigns value of "Hello there world!" without quotes.

Examples:

variable comment
$var1 = 'PHP'; Assigns value of "PHP" to var1
$var2 = 5; Assigns value of 5 to $var2
$var3 = $var2 + 1; Assigns value of 6 to $var3
$var2 = $var1; Assigns value of "PHP" to $var2
echo($var1); Outputs "PHP"
echo($var2); Outputs "PHP"
echo($var3); Outputs 6
echo($var1 . ' rules!'); Outputs "PHP rules!"
echo("var1 rules!"); Outputs "PHP rules!"
echo('var1 rules!'); Outputs '$var1 rules!'

NOTE: Double quotes around text enables the variable name to be inserted & value is displayed. This is called variable interpolation. Single quotes will not interpolate the variable name.

Arrays

A special variable that contains multiple values.

Simple format:

$array_name = array('test1', 'test2', 69);

To retrieve a value stored in an array you need the index.

Generally, arrays use incrementing integers that start with zero as their indices. Indices act as pointers to precisely locate a value in an array.

Expanding on simple format:

code comment
echo($array_name[0]); Outputs 'test1'
echo($array_name[1]); Outputs 'test2'
echo($array_name[2]); Outputs 69