CSCTR 000
CSCTR 000
6. It is easy to learn
BASIC SYNTAX
3. <script langage="php"
echo "Hello World";
</script>
1. integer
2. Floating-point numbers
3. string
4. array
5. object
INTEGER
$a = 1.234;
$a = 1.2e3;
STRING
\n linefeed
\r carriage return
\t horizontal tab
ARRAYS
class a
{
var $var="hello";
function echo_hello()
{
echo $this->var;
}
}
$a=new a();
TYPE CASTING
2. echo $a $hello;
CONSTANTS
1. APACHE VARIABLES
2. PHP VARIABLES
APACHE VARIABLES
REQUEST_METHOD
GET, HEAD, POST, PUT.
QUERY_STRING
The query string, if any, via which the page was
accessed.
DOCUMENT_ROOT
The document root directory under which the
current script is executing, as defined in the server's
configuration file.
HTTP_USER_AGENT
Contents of the User_Agent
PHP VARIABLES
PHP_SELF
The filename of the currently executing script.
(not for cli)
$_COOKIE
An associative array of variables passed to the
current script via HTTP cookies.
$_GET
An associative array of variables passed to the
current script via the HTTP GET method.
$_POST
An associative array of variables passed to the
current script via the HTTP POST method.
$_FILES
An associative array of variables containing
information about files uploaded via the HTTP
SCOPE OF VARIABLE
$visible=10;
function test()
{
print $visible;
}
test();
function test()
{
global $visible;
print $visible;
}
test();
function test()
{
print $GLOBALS['visible'];
}
test();
OPERATORS
1. Arithmetic Operators
2. String Operators
3. Assignment Operators
4. Comparison Operators
5. Logical Operators
6. Bitwise Operators
7. The Ternary Operator
8. The Error Control Operator
9. The Execution Operator
BITWISE OPERATORS
bitwise OR |
bitwise NOT ~
bitwise XOR ^
'+' - Addition
'-' - Subtraction
'* - Multipication
'/' - Division
'%' - Modulus
COMPARISON OPERATORS
== equals
=== identical
!= not equal
<> not equal
< less than
> greater than
<= less than or equal to
>= greater than or equal to
LOGICAL OPERATORS
NOT !
AND &&
OR ||
BITWISE OPERATORS
bitwise OR |
bitwise NOT ~
bitwise XOR ^
Assignment Operators
The '=' is used as the assignment operator.
$a+=5;
which is equivalent to
$a=$a+5;
1. gettype(parameter)
2. settype(variable name, datatype as string)
3. is_int()
4. is_float($a)
5. is_string($a)
6. is_object($a)
7. int isset($a)
8. int isset($a)
9. int empty($a)
10. intval($a)
11. doubleval($a)
12. strval($a)
CONTROL STRUCTURES
1. If-ElseIf-Else
2. while
3. Switch
4. break
5. continue
6. exit
7. for
8. foreach
9. include statements
IF-ELSEIF-ELSE
if ($a>$b)
{
echo "a is bigger than b";
}
elseif($b>$a)
{
echo "b is bigger than a";
}
else
{
echo "a and b are equal";
}
SWITCH
switch($gender)
{
case 'male' :
echo "gender is male";
break;
case 'female' :
echo "gender is female";
break;
default :
echo "none of the above";
break;
}
WHILE
$i=0;
while($i<10)
{
echo $i;
$i++;
}
$a=10;
do
{
echo $a;
$a--;
} while ($a > 1 );
BREAK
$a=0;
while($a<10)
{
$a++;
if($a==5)
{
break;
}
echo $a;
}
CONTINUE
$a=0;
while($a<10)
{
$a++;
if($a==5)
{
continue;
}
echo $a;
}
EXIT
$a=0;
while($a<10)
{
$a++;
if($a==5)
{
exit;
}
echo $a;
}
FOR
1. require (“connect.inc”);
2. include (“connect.inc”);
3. include_once(“connect.inc”);
4. require_once (“connect.inc”);
FUNCTIONS
function name_of_function(arg1,arg2,......)
{
}
FUNCTION NAME
2. function square($a)
{
$b=$a*$a;
return $b;
}
FUNCTION CALL
1. square();
2. $c=square(5);
echo $c;
3. echo square(5);
SCOPE OF VARIABLE
1. A variable defined inside a function is visible only
within that i.e., the scope of the variable is local inside
a function.
function factorial($n)
{
if($n==1)
return 1;
else
return $n*factorial($n-1);
}
echo factorial(5);
PASS BY REFERENCE
$a=5;
echo factorial(&$a);
echo $a;
ARRAYS
$arr=array("flower","fruit","numbers");
while(list($key,$val)=each($fruits))
{
echo "key: $key; Value: $val\n";
}
MULTIDIMENSIONAL ARRAY
$employees = array(
array( "ram","manager", 10000 ),
array( "sam", "executive", 15000 ),
array( "raj", "vp", 40000 )
);
ARRAY FUNCTION
1. Sorting Arrays
$arr=array("flower","fruit","numbers");
sort($arr);