Академический Документы
Профессиональный Документы
Культура Документы
Script Style:
<SCRIPT LANGUAGE='php'> PHP statement; </SCRIPT>
Comments
<html> <body> <?php //This is a comment echo "Normal Comment"; /* echo is a function that outputs something on the web page */ echo "Block comment"; # We shall talk about it later ?> </body>
Variables
Do not require to declare variable type; Simply assign a value and you can use the variable $name = Tom // this is string type $age = 20 // this is integer type Constants: define(PRICE', 100); There are two type of variables Local: can be accessed within a block Global: can be accessed throughout the file/system Local/Global these are called scopes
Operators
Pretty much same as in other programming languages (C, Java, etc.) Arithmetic operators: +,-,*,/, % etc Increment operators: ++, -- etc Assignment operators: =, +=, -=, *=, /= etc Comparison operator: ==, >=, <=, != Logical operators: &&, ||, !
Concat operator
PHP has a special operator . which is used as a concatenation operator for strings We will be relying on this operator most of the time More on this in the next sides
String manipulation
Let us check out the example:
<?php $txt1=Hello; $txt2= World; $txt3 = $txt1 . " " . $txt2 echo $txt3; ?>
Conditional execution
If-else
Executes a block of code upon specific criteria
If(condition is true){ execute something }else{ execute something else }
IF-else
<html> <body> <?php $name = John; if ($name == John) // look clearfully the use of == echo Welcome John; else echo Who are you?; ?> </body> </html>
If-else if - else
<html> <body> <?php $name = John; if ($name == John) // look clearfully the use of == echo Welcome John; else if($name == Tom) echo Welcome Tom; else echo Who are you?; ?> </body> </html>
Array
Create an array (one dim array): $products = array ('Tires', 'Oil', 'Engine'); Automatically generate sequnces of number, character: $numbers = range (1,10,2) // range() is a function Accessing element: $products[0] Array with different indices (associative array): $prices = array( 'Tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4 );
Array (contd.)
Multidimensional Array: $products[row][column] $products = array( array( 'Code' => 'TIR, 'Description' => 'Tires, 'Price' => 100 ), array( 'Code' => 'OIL, 'Description' => 'Oil, 'Price' => 10 ), );
Loops
Looping means doing same task over time There are mainly thee kinds of looping is PHP
for while do ... while
For loop
For loop works as follows:
for (initialization; condition; increment) code to be executed; } Let us clarify this with an example:
While Loop
In contrast to for loop, while loop has a slight different structure, although they both do the same work While loop works as follows: while (condition)
code to be executed;
Function
New function: function my_function(){ echo 'My function was called; } Calling function my_function();
Function (contd.)
PHP has a lot of built in fuctions
Date(), Time() time/date function echo, sprintf(), strpos(), strtolower, strtoupper(), str_replace(), substr(), explode(), trim() string handling functions Fopen(), fclose() file handling functions