Você está na página 1de 3

Introduction: PHP is an acronym for "PHP Hypertext Preprocessor", open source cross-platform scripting language (scripts that are

executed on the server and result is returned to the browser as plain HTML). Requirement: A Web Server (such as Apache), A Database Server for DB (such as MySQL) and PHP. Two widely used All in One Server: XAMPP and WAMP. PHP Syntax: <? php ?> PHP Script starts with <?php and ends with ?>. The script can be placed anywhere in the HTML/CSS document. Default File Extension: php Statements are terminated with Semicolons . Functions has and opening and close braces. PHP Comments: Single Line (// or #), Multiline (/* comments */) All functions, classes, variables and keywords are case sensitive. Assignment Operators x=yx=y x += y x = x+y x -= y x = x-y x *= y x = x*y x /= y x = x/y x %= y x=x%y String Operators . (Concatenation) .= (Concatenation Assignment) Ex: $var1 = $var2 . " World"; Comparison Operators == Equal === Identical != Not equal <> Not equal !== Not identical > Greater than < Less than >= Greater than or equal to <= Less than or equal to Mathematical Operators Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus or Remainder (%) Increment/Decrement Operators ++$var (Pre Increment) --$var (Pre Decrement) $var++ (Post Increment) $var-- (Post Decrement) Logical Operators

and (&&) or (||) xor not (!)

Variables & Constants: Unlike other programming languages, declaration of data type of a variable is not required in PHP, rather it automatically converts to the appropriate data type depending on the assigned value. A PHP variable starts with $ sign followed by the name of the variable, where the name cannot start with a number and must start with a letter or underscore. The name can only contain alpha-numeric characters and underscores and they are case sensitive. There are three scope of variables in PHP: Local, declared within a function and can only be accessed within that function Global, declared outside a function and can be accessed directly outside a function or through calling $GLOBALS ('variable') inside a function. From within a function, global variables are accessed either by using the keyword global or by calling the $GLOBALS[index]. Static, retains the value in the variable even after completing the function. However, the variable is still local to the function.

To set a constant, we use the define() function - it takes three parameters: The first parameter defines the name of the constant, the second parameter defines the value of the constant, and the optional third parameter specifies whether the constant name should be case-insensitive. Default is false.
<?php $globalVar1=5; $globalVar2=10; define("SHOWLABEL", "Function Call # "); function TestVariable() { global $globalVar1,$globalVar2; static $localCallNo = 1; $globalVar2=$globalVar1+$globalVar2; $GLOBALS['globalVar2']=$GLOBALS['globalVar1']+$GLOBALS['globalVar2']; echo SHOWLABEL, "$localCallNo | Value: $globalVar2 <br>"; $localCallNo++; } TestVariable(); TestVariable(); TestVariable(); TestVariable(); TestVariable(); ?>

Three types of Arrays: Indexed arrays - Arrays with numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays In Indexed arrays, the index can be assigned automatically (index always starts at 0): Ex: $cars=array("Volvo","BMW","Toyota"); or in Associative arrays, the index can be assigned manually. Ex: $cars[0]="Volvo"; $cars[1]="BMW"; $cars[2]="Toyota"; Assigning values to associative arrays: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); $age['Peter']="35"; $age['Ben']="37"; $age['Joe']="43";

Você também pode gostar