Вы находитесь на странице: 1из 43

Learning PHP Part 1

by Nulled Byte on Sunday, 25 September 2011 at 21:02


mmso.org php tutorial

PHP Learning Part 1
php tutorial
l
!T Field tag
l deactivate
tag l
tag l






Chapter I (Introduction of PHP)
PHP Personal Home Page l
Pre Hypertext Processor (Hypertext Pre Processor)
l PHP Dynamic Web Site l
Server Side Scripting Language

PHP Tags
PHP Tag +

Long Tag (or) Standard Tag


?>

Short Tag

?> Alternative Long Tags <script
language=php></script> ASP Style Tag
echo l Eg~
echo command print command

; statement
statement ;
Eg~ phpprint "Hello World";?> <script></script>

PHP Tag statement ;

Eg~

PHP Tag multiple statement statement
;
Eg~ Chapter II (Comments) Comments PHP
Coding Coding
comment
Page Browser


PHP comments 3
v C Style commentv C++ Style Commentsv Unix/Linux Style Com
ments C Style Comments/**/
C Style Comments comments
Nultiple Lines

Eg~
C++ Style Comments
//

comment
l //
Eg~ Unix/Linux Style Comments#

comment
l #
Eg~ Chapter III (Variables) variable value
l special container
programming fundamental l

v Numbersv strings of charactersv objectsv Arraysv
Booleans

hold l variable $ l


Types of Variables that PHP supports
PHP l variable type 8 support v Booleans -
> True/Falsev Integers -> (decimal, octal, hex)v Floats -> 2.9,3.0v Strings -
> (, , here documents (allows strings are multiple lines))v
Arrays -> similar to dictionaries in Python and hashes in Perl
(It provides key/value Pairs)v Objectsv NULL -
> Special Data Type (An uninitialized variable)v
Resource -> Special Data Type (Reference to a third party resource)
(Eg~ Database or etc..)
Remark
PHP Variables must be named with alphas and/or underscore

Eg~ $first_var = Data;$second_var = Data;
variable value hold
l special container
ll value
Cyber, Hunter, Student, 25
variable

value variable type
l Cyber, Hunter,
Student value hold variable
$first_name, $last_name, $occupation variable strings
25 value hold $age
variable integer l
l integer $age variable
25 value strings value
' ', " " quote
l variable
echo variable l
space l "\t" tab
l coding
ll Coding
echo $first_name, \t, $last_na
me, \t, $occupation, \t, $age;
Space l "\t" " "
Eg~ echo $first_name, " ", $last_name, " ", $occupation, " ", $age;
$first_name variable l $last_name variable
space 2 variable
space
l


" " value variable interpolation ' '
value non variable interpolation

Eg~ $var1 = 'Cyber';$var2 = 'Hunter';$var3 = 'is a Student';$age = 25;

Cyber Hunter is a Student
25
output
echo $var1, "\t", $var2, "\t", $var3, "\n", $age;
l
Cyber Hunter is a Student
25
Browser
echo $var1, '\t', $var2, '\t, $var3, '\t', $age;
l
Cyber\tHunter\tis a Student\n25 Browser
Chapter IV (Basic for Strings) String
PHP Script
coding Browser
Hello World. I am Cyber Hunter. I love PHP.
l Chapter V (Basic of integers) integer
PHP file
string value l
output l
calculation script
echo $num1; echo echo 10; l
l $num1 variable l $num2
variable script ll $num1
variable l $num2 variable
result browser
variable l l
10 l
20 result browser
$result v
ariable $num1 l
$num2 l $result variable
result
coding $num1 variable 10
$num2 variable 20
$num1 l $num2 l
$result $result
variable echo l
30 result l



multiply
$num1 variable l hold
2 l $um2 variable l hold
3 l multiply l
result browser
value multiply
value divide

coding num1 variable value
6 num2 variable value 3
l divide result browser

/ Arithmetic Operator
10/2 10
2 ll 5
l 5%2
l 1 result
l l

E.g~
coding $num1 $num2
l l
% Arithmetic Operator
l Browser
1 l Arithmetic Operator


+++|Arithmetic Operators|+++
Operator Definition Example Result of Example

v + Addition 1+1 2v - Subtraction 2-
1 1v * Multiplication 2*2 4v / Division 10/2 5v % Modulus
5%2 1 Chapter VI (Other Variables)

Here documents
l multiple line create

Eg~ SQL quries (SQL statements)
here document php script

Eg~It's used to see multiple lines.Hopefully that you'll understand.cyberhunter;echo $va
rhere1;?>
script browser

This is the first line of here document.
It's used to see multiple lines.
Hopefully that you'll understand.
here document cyberhunter
l cyberhunter keyword l
cyberhunter keyword l
interpreter l variable hold value


cyberhunter keyword
line l ll
space here document
standard syntax script
l HTNL
enter l


here document variable interpulation

here document PHP
scripting language BASH, Perl, Python scripting
language support

Booleans
Boolean TruefFalse value condition
True or False


Unix Shell 0 True
Programming environment

boolean
statement
condition


Arrays
Arrays variable type Arrays
Perl l hashes l Python l dictionaries
Arrays keyfvalue Pairs provide
( Perl l hashes l Python
l dictionaries keyfvalue Pairs store )
variable value hold
l special container
variable
value store Arrays
multiple values variable
store

Chapter VII (Operators & Exp
ressions)
Operators multiple value
value l
operator value l
value
operator l operate
l value operand l

2+3
2 l 3 value operand
l value l
operate operator + Operators
operand






coding
l coding $var1 variable
type l gettype() function
browser $var1 variable type
integer


2+3 2+3
2 expression 3 expression +
operator expression


Assignment Operator
Assignment Operator = single character
assignment operator operand value
operand
l

Eg~ coding operand value
user $var1 operand variable
echo $var1 e
cho user browser

coding user value var1
variable echo
l

The Concatenation Operator
Concatenation Operator (.).
operand
echo "Hello World";
ll Concatenation Operator
echo "Hello"."World"; echo
ll


Eg~ ff The following is the alternate way of the above code coding
$meter variable 15000 value hold
l kilometer 1
kilometer 1000 meters 15000 value
1000 l kilometer echo


assignment operator
$x = 3;
assignment operator l
echo $x = x + 3; echo
x variable l hold value l 3 value
l browser
l result 6


Eg~ coding $x = 3; value x
operand assign echo $x + = 4;
echo $x value ope
rand value + l
result l 7 l
Combined Assignment Operator
l v += $x += 5
$x = $x + 5v -= $x -= 5 $x = $x -
5v /= $x /= 5 $x = $x / 5v *= $x *= 5 $x = $x
* 5v %= $x %= 5 $x = $x % 5v .= $x .= " test" $x =
$x." test"
Comparison Operators
Comparison Operator statement condition
loop
statement condition l loop
Comparison Operator
statement condition l code block
loop
Definition
Comparison Operators

v == Equivalence
Left value is equivalent to right value
v != Non-equivalence
Left value isnt equivalent to right value
v === Identical
Left is equivalent to right and they are the same type
v > Greater than
Left is greater than right
v Less than
Left is less than right
v >= Greater than or equal to
Left is greater than or equal to right
v Less than or equal to
Left is less than or equal to right Chapter VIII (Constants)
Constants variable value define
variable value change
l
Eg~
coding $var1 value Cyber Hunter
$var1 value Terminator
$var1 value
Terminator
l
constants
l define() function
l syntax define ("Syntax_Name", "value");

Eg~ coding var1 value Cyber Hunter
var1 valu
e Terminator define
var1 l constant value Cyber Hunter
l Terminator
echo c
onstant value Cyber Hunter browser
Chapter IX (Switching Flow)
PHP file
output l
PHP file dynamic l if
statement ll
if Statement
if Statement l statement expression true value
statement
statement
if statement syntax
if (expression){/*code to execute if the statemen
t is true*/}
l
Eg~
coding $user variable value Cyber
Hunter $user
Cyber Hunter value l This user is Cyber Hunter
echo l condition statement
condition $user variable
value Cyber Hunter ll
This user is Cyber Hunter
Cyber Hunter l This user is Cyber Hunter
output
l
else
clause if statement l
$user value Cyber Hunter This user is unknown
user output

Eg~ coding $user value Cyber Hunter
if Statement $user value user10
expression
This user is Cyber Hunter
statement expression
This user is unknown user else
l This user is unknown user
else clause l statement

else if clause if else
l
expression else if l

else process l syntax

if(expression1){/*code to execute if the statement is true*/}el
se if(expression2){/*code to execute if the previous statement is false and this stateme
nt is true*/}else{/*code to execute none of the above statements are false*/}
l
Eg~
Switch Statement
Switch Statement if Statement
if Statement l Switch
Statement l
expression l case l
l l
l


Switch Statement l syntax

switch (expression) { case result1: // exec
ute this if expression results in result1 break; case result2: // execute this if ex
pression results in result2 break; default: // execute this if no break statement
// has been encountered hitherto}
l

Eg~
coding $cyberhunter value Site Admin
Noderator, Global Noderator value l
$cyberhunter l This member is in the Noderator Group echo
value l
v!P, Contributor case
Banned, Spammer l
Administrator, Site Admin, Board Admin
l This member is in
the Administrator Group l
l Sorry, this user haven't registered yet
break;
statement

if Statement condition expression
Switch Statement Nultiple condition
expression l


? Operator (ternary Operator)
? Operator (ternary Operator) if Statement l
l syntax

(expression) ?returned_if_expression_is_true:returned_if_expres
sion_is_false;
l

Eg~ coding $cyberhunter value Site Admin
$user l $admin l value d
efine $result ($cyberhunter=="Normal User") ex
pression $cyberhunter value Site Admin
expression $user return
$admin return ($cy
berhunter=="Normal User") $admin
return $result echo
$admin return This user is
Site Admin browser Chapter X
(Loops)
Loop code execute

task repeat

loop statement while Statement
l
while Statement
while Statement syntax if Statement l while
Statement statement expression
l code operate
loop code block execution
iteration l while Statement syntax
while ( expression ){ // Code to execute}
l
Eg~ ";$number++;}?>
coding $number = 1;
integer $number
$number 12 l 12
l $number*2 execute
l
$number++ $number 1
l
iteration $number*2 1 2
l 2 echo
statement $number++
$number value 1 1
2 12
loop
iteration $number 1
2
$number*2 $number value 2
2 l + statement
statement $number++
$number value 2 1 3
value 12 loop
l $number 12
l loop
result browser

Results :-

1 x 2 is 2
2 x 2 is 4
3 x 2 is 6
4 x 2 is 8
5 x 2 is 10
6 x 2 is 12
7 x 2 is 14
8 x 2 is 16
9 x 2 is 18
10 x 2 is 20
11 x 2 is 22
12 x 2 is 24

do while Loop
do while Loop while loop l while
loop l condition code execute
while expression
execute
l syntax
do { // code to be executed} while (expression
);
l
Eg~ \n";$num++;}while ( $num > 200 && $num
coding $num = 1; integer
do The value
of the number is: $num $num echo
while loop $num > 200 88 $num <
+00 l $num 200
+00 condition
$num 1
while loop
condition The value of the number is: 1
browser
l $num 201 201
399 browser
do $num $num
echo $num++
$num 1
while statement
$num 202 condition
$num 202
echo $num++
202 1 203
203 while 200
+00
loop l 399 loop
browser result





for statement while loop l
while loop l for loop l data initialization condition
testing modification expression
l while loop
value loop
while l condition code
block loop loop code block
modification expression for loop process
l
code block loop
l l syntax
for ( initialization expression; test expression; modificatio
n expression ){ // code to be executed}
l

E.g~ ";}?>
coding for l $number = 1;
$number variable 1 value
$number
coding run browser

Results :-

1 x 2 is 2
2 x 2 is 4
3 x 2 is 6
4 x 2 is 8
5 x 2 is 10
6 x 2 is 12
7 x 2 is 14
8 x 2 is 16
9 x 2 is 18
10 x 2 is 20
11 x 2 is 22
12 x 2 is 24

Using the break Statement to end the loop
break statement
break statement loop
l for (x = 1; x
E.g~ ";)?> coding l for
($x = 1; $x

Using the continue Statement to skip the iteration
break statement loop terminate
continue
statement loop

break
looping terminate continue looping
loop
l continue
l

E.g~ ";}?>
coding for ($x = -5; $x "; 100
$x value l l $y value
ll l
100 -5, -+, -3, -2, -1 l
-1 l 0
100f0 error
(mathematics 0
ll) error
for loop if ($x == 0) {
continue; ) 100 0 l iteration
iteration state

Nesting 2 loopings
iteration iteration
l browser
ll
E.g~

I want as follows :
12345
12345
12345
12345
12345

coding ?
looping looping
coding
E.g~ ";while ($x ";$x++;}?>
coding x
alphabet l l echo
l l
$x = 1; $x value 1 l
while loop while ($x

while loop loopA l
loopA while loop loopB
l coding ll $x
1 loopA while ($x ";
l br
$x++; $x
1 2 2
loopA condition 5
loopA code block
loopA code block $y = 1; while ($y
Result:-
I want as follows :

12345
12345
12345
12345
12345
coding for loop l
syntax concept

for loop

self creation

E.g~ ";for ($x = 1; $x ";}?>

browser
l coding
l concept
advanced

Question :-

I want as follows :
1
12
123
1234
12345

self creation
self motivated coding

Coding :- ";$x = 1;while ($x ";$x++;}?>
coding for loop ll

l

browser output
exercise
advanced l
Question :-
I want as follows :

12345
1234
123
12
1
l

Answer :- ";$x = 5;while ($x >= 1){$y =1;while ($y ";$x--;}?>
browser result
exercise
advanced


Question :-
I want as follows :

54321
5432
543
54
5
l

Answer :- ";$x = 1;$z = 5;while ($x = $x){echo "$y";$y--;}echo "";$x++;}?>
hapter XI (Functions)

Function machine
machine
material
output








( )


Iunction
materials data
command script
Iunction data
execute Iunction
certain result generate
Iunction concept


Iunction built-in function user-defined function

Built-in functions
built-in Iunction
gettype(); Iunction built-in Iunction
code
Code:
$var1 =1;
echo gettype($var1);
?>

coding $var1 value 1
1 integer
$var1 data type
echo gettype($var1);
$var1 variable gettype() Iunction
echo $var1 data type integer
browser
integer echo $var1; echo
$var1 value 1


gettype() Iunction ()
parantheses data
inIormation
arguments Iunction
argument argument
,
syntax

Syntax :-
function_name ($argument_one, $argument_two);
built-in Iunction abs() Iunction

Eg~
Code:
$var1 = -123;
$var2 = abs( $var1 );
echo $var2;
// it will show "123"
?>

$var1 = -123;
$var2 variable value $var1
abs() function result
$var2 assignment operator
$var2
echo abs() Iunction process
generate result 123
value browser
-123 value hold $var1 abs()
Iunction -123 data
process coding

Eg~
Code:
$var1 = -123;
$var2 = abs( -123 );
echo "$var2,
";
// it will show "123"
echo gettype($var2), "
", gettype($var1);
?>

value variable assign Iunction
coding
coding

Eg~
Code:
echo (abs(-123));
?>
built-in Iunction

user-deIined Iunction


User-defined functions
Iunction syntax

Syntax :-
Code:
function function_name( $argument1, $argument2 )
{
// code for function
}

Iunction Iunction statement
functionname Iunction
argument argument ()
parentheses
argument Iunction

Eg~
Code:
function hello_world()
{
echo "Hello World.!";
}
hello_world();
?>
helloworld() Iunction execute
code block echo "Hello World.!"; echo
helloworld() Iunction
execute helloworld()
Iunction echo "Hello World.!"
process
browser Hello World.!
Iunction
argument code block



Iunction argument code
block
Eg~
PHP Code:

function sentence_function( $var1 )
{
echo ( "$var1
\n" );
}
sentence_function("Sentence1");
sentence_function("Sentence2");
sentence_function("Sentence3");
sentence_function("Sentence4");
sentence_function("Sentence5");
?>
coding sentenceIunction
Iunction $var1 argument
sentenceIunction code block $var1 echo br
sentenceIunction $var1
argument Sentence1 $var1
value Sentence1 sentenceIunction function
execute sentenceIunction code block
echo ( "$var1
\n" ); echo $var1 value
Sentence1 Sentence1
echo

sentenceIunction process Iunction argument
$var1 value Sentence2 assign
sentenceIunction code block Sentence2
echo Sentence3, Sentence4,
Sentence5 echo
sentenceIunction () parentheses
data sentenceIunction
$var1 argument assign


Returning the Values
value return
advanced

Iunction code block data return


Iunction Iunction integer
calculate Iunction
calculate value value
calculate value calculate
result
coding
PHP Code:

function calculator( $var1, $var2 )
{
$result = $var1 + $var2;
return $result;
}
echo calculator(1,2);
?>

coding
calculator Iunction
Iunction
$var1 $var2 argument
function code block $var1 $var2
$result
$result $var1 $var2;

$var1 $var2
$result
$result return
calculator Iunction code block
browser certain result
Iunction
data


material
output
certain result
Iunction data
echo calculator (1, 2); 1
value $var1 assign 2 value
$var2 assign calculator
Iunction code block
$var1 value 1
$var2 value 2 value
$result
variable assign $result
return step by step process
certain output 3 value
browser

return


return value process
return
result
return $result
$result value return
$result $var1
$var2 $var1
Iunction
1 $var2 2
$var1 + $var2 $var1
1 $var2 2
3 result 3 result
$result return $result $result
return 3 output

$result return
5 value return
browser 5
return 5; 5 value


return $result; return ($var1/$var2);

PHP Code:

function calculator ($var1, var2)
{
$result = $var1 + $var2;
return ($var1/$var2);
}
echo calculator (1, 2);
?>

coding return ($var1/$var2);
$var1 $var2
1 2
result 0.5 browser
return

return Iunction return

PHP Code:

function return_function ($var1, $var2)
{
$result = $var1 + $var2;
echo $result;
}
coding returnIunction
Iunction Iunction return
coding
PHP Code:

function return_function($var1, $var2)
{
$result = $var1 + $var2;
echo $result;
}
return (return_function (1, 2));
?>
return (return_function(1, 2));
return_function Iunction return Iunction $var1
$var2 1, 2
assign result 3
Iunction
return
syntax
PHP Code:
return ( function_name( $argument ) );




Dynamic Function Calls
Iunction variable
Iunction name variable value

variable Iunction
coding

PHP Code:

function function_one()
{
echo "Hello World";
}
$var1 = "function_one";
$var1();
?>

coding function_one Iunction
Hello World echo
Iunction
$var1 = "function_one"; Iunctionone
$var1 value assign $var1
Iunction
$var1(); functionone code
block process Hello World
browser

script
PHP Code:

function function_one()
{
echo "Hello World";
}
$var1 = "function_one";
return ($var1());
?>
coding



variable
scope Iunction declare
variable Iunction
Iunction Iunction
variable

coding

PHP Code:

function function_one()
{
$var1 = "This is Variable 1";
}
echo "Testing : $var1";
?>
coding functionone() Iunction $var1
variable declare Iunction
$var1 echo $var1 value
Testing:

$var1 Iunctionone Iunction
Iunction
echo
output result
Iunction declare variable
Iunction

coding

PHP Code:

$var1 = "This is variable1";
function function_one()
{
echo "Testing: $var1";
}
function_one();
?>

coding


Iunction
declare variable Iunction


global
statement
coding

PHP Code:

$var1 = "Hello Word";
function function_one()
{
global $var1;
echo "Greeting from the script : $var1";
}
function_one();
?>

coding $var1 value Hello World.!
assign function_one()
Iunction Iunctionone code block
$var1
global $var1; global
statement echo
Iunction process
process result Greeting
from the script : Hello World.! browser
programmer


global statement
variable
variable Iunction
Iunction
variable value
$var1 1;
Iunction global $var1; $var1;
$var1 value $var1;
$var1 value 1 1
$var1 value 2


coding
PHP Code:

$var1 = 1;
function function_one()
{
global $var1;
$var1++;
echo $var1, "
";
}
function_one();
function_one();
?>
coding $var1 =1;

function_one() Iunction Iunction
code block global $var1; $var1
$var1++;
$var1 1
echo $var1 $var1 echo
function_one();
Iunctionone Iunction
$var1 1
1 1 2
2 $var1
$var1 echo 2 br
Iunctionone(); Iunction
$var1
2 2 1 3
echo 3

code block

PHP Code:

$var1 = 1;
function function_one ($var2)
{
global $var1;
echo "$var1."." "."$var2", "
";
$var1++;
}
function_one ("This is line 1");
function_one ("This is line 2");
function_one ("This is line 3");
?>



static statement
static variable value

global statement declare
variable Iunction local variable

coding
PHP Code:

$var1 = 1;
function function_one ($var2)
{
static $var1 = 1;
echo "$var1."." "."$var2", "
";
$var1++;
}
function_one ("This is line 1");
function_one ("This is line 2");
function_one ("This is line 3");
?>
coding $var1 value 1
static $var1 = 0;
$var1 0

coding
PHP Code:

$var1 = 1;
function function_one ($var2)
{
static $var1 = 0;
echo "$var1."." "."$var2", "
";
$var1++;
}
function_one ("This is line 0");
function_one ("This is line 1");
function_one ("This is line 2");
?>



PHP Code:

function function_heading( $var1, $var2 )
{
echo "<h>$var1</h>$var2>";
}
function_heading("Sentence1", 1);
function_heading("Sentence2",2);
function_heading("Senteence3",3);
?>
coding
function_heading() Iunction
Iunction $var1 $var2 argument
Iunctionheading Iunction code
block echo "<h>$var1</h>$var2>"; echo
echo statement
h~
html tag h~ $var2
$var2
h~
$var2
1



data

$var2

<h>$var1</h>$var2);


function_heading("Sentence1",1);
Sentence1
data $var1 assign 1
data $var2 assign
<h>

heading size
heading size




</h></h></h></h>
PHP Code:

function function_heading( $var1, $var2=3 )
{
echo "<h>$var1</h>$var2>";
}
function_heading("Sentence1", 1);
function_heading("Sentence2",2);
function_heading("Sentence3");
function_heading("Sentence4");
?>
coding
function function_heading ($var1, $var2 = 3) Iunction
Iunction argument $var1 $var2
argument $var2 3
$var2 argument value

Iunction call
$var2 value
value
assign value deIault

function_heading("Sentence1", 1);
$var2 1

function_heading("Sentence3");
$var2 argument value
$var2 default value
3 $var2 argument assign

Вам также может понравиться