Page 1

Basic PHP Tutorial

www.m51a.com


What is PHP?

Rasmus Lerdorf (born November 22, 1968, in Qeqertarsuaq, Greenland

၈ ၇ ၉၉၅


၉၉၈

Andi Gutmans

Zeev Suraski

အ အ

Hyper

PH

Dynamic & Interactiv

PHP

။ Server Sit

Informix, Oracle, Sybase, Solid, PostgreSQL, Generic, ODBC, etc …

။ အ

- MySQL,


။ အ

Wordpress,

Where to Start?

LAMP (Linux Apache MySQL PHP)၊ WAMP (Window

Apache MySQL PHP)၊ XAMPP (X (meaning cross-platform) Apache MySQL PHP Preal) အ


WAMP Server Installation



.



P

PHP

Tag (၄)

T

။၄

-

T

T

<?php

?>

<?php

?>

၂။

T

T

<?

?>

<?

?> အ


၃။

T

T

<%

%>

<%

%>

၄။

T

T

<script language=”PHP“>

</script>

<script language=”php”>

</script>


Output text with PHP

semicolon (;

-

<?php

echo “Hello, Friend”;

print “Hello, World”;

?>

echo

Syntax ျဖစ္သည့္အတြက္ေၾကာင့္ ျဖစ္ပါသည္။

print


<?php

echo “ Hello Friend, “ Nice To Meet U “” ;

?>

T

”အ

။အ

“ အ

“ ”

“”

-

<?php

echo „ Hello Friend, “Nice To Meet U”‟ ;

?>

“” အ

„‟


-

<?php

echo “Hello Friend, \“ Nice To Meet U\ “” ;

echo „ I\‟m Just ducky. ‟;

?>


Comments in PHP


။ အ အ

။ Single Line Comments

၂။ Multi Line Comments

။ Unix/Linux shell-Style(Prel style) Comments

Unix/Linux shell-Sty


<?php

# Output “Hello Friend” (this is single line comment)

echo “Hello, Friend”;

?>

၂။ C++ Style Comments

C++ Style Comments

<?php

// Output “Hello Friend” (this is single line comment)

echo “Hello, Friend”;

?>


၂ ။ C Style Comments

<?php

/* Output “Hello Friend” (this is Multi line comment) */

echo “Hello, Friend”;

?>


Variables in PHP

-

-

-

PHP Variable Types

T

။ Integers: Are whole numbers, without a decimal point, like 4195

၂။ Doubles: Are floating-point numbers, like 3.14159 or.49.1


၃။ Booleans: have only two possible values either true or false

၄။ NULL: Special Data Type (An uninitialized variable)

၅။ Strings: („‟, ””, here documents (allows strings are multiple lines))

၆။ Arrays: similar to dictionaries in Python and hashes in Perl

(It provides key/value Pairs)

၇။ Objects: are instances of programmer-defined classes, which can package up both other kinds of values

and functions that are specific to the class.

၈။ Resources: Special Data Type (Reference to a third party resource)

<?php

$txt=”Welcome to M51A”;

echo $txt;

?>

-


Va

။အ

( . ) Operator

-

<?php

$a="I Love Mistake";

$b="U are Welcome";

echo $a.$b;

?>


-

<?php

$first_name= „Mistake‟;

$second_name= 51;

$last_name= „Area‟;

echo $first_name, “\t”, $second_name, “\t”, $last_name;

?>

vari


<?php

$first_name= „Mistake‟;

$second_name= 51;

$last_name= „Area‟;

echo $first_name, „\t‟, $second_name, „\t‟, $last_name;

?>

PHP Script

Answer: 5 + 4 = 9

<?php


$a = 5;

$b = 4;

$c = 9;

echo 'Answer:',"\t", $a,'+', $b,' =',"\t",$c;

?>

<?php

$a = 10000;

$b = 99999.99;

echo ($a+$b);

?>

()


Arithmatic Operators

-

။--

--

Comparison Operators

(


Programming Looping in PHP

။အ

repeat

။ While Loops

၂။ Do While Loops

၃။ For Loops

၄။ Foreach Loops


။ While Loops

while ( expression )

{

// Code to execute

}

-

<?php

$online = 1;

while ( $online <= 12 )

{

echo "$online x 2 = ".($online*2)."<br />";


$online++;

}?>

။အ

$counter = 1;

while ($counter < 11)

{

echo ("counter =".$counter."<br>");

<?php

-


$counter++;

}

?>

T

<br>

၂။ Do While Loop


{

// code to be executed

}

while (condition);

-

<?php

$upperlimit = 10;

$lowerlimit = 1;

do

{

echo ($upperlimit * $lowerlimit) . '&nbsp;';

$lowerlimit++;

}


while ($lowerlimit <= $upperlimit);

?>

Space

T

<?php

$counter = 1;

do

{

echo "The number is: $counter <br />\n";

$counter ++;

upperli


}

while ( $counter > 500 && $counter < 1000 );

?>

cond

၉၉၉

T

၅ ၂

၅ ၂

။ ၅ ၂

၅ ၃

။အ

၉၉၉


၃။

For Lo

for ( initialization ; condition; increment)

{

// code to be executed

}

-

<?php

for ($number = 1; $number <= 12; $number++)

{


echo "Welcome to M51A!<br />";

}

?>

၄။

foreach (array _variable as value)

{

//code to be executed

}

foreach (array _variable as $key => $value)

{


//code to be executed

}

<?php

$test=array(1,2,3,4,5,6,7,8,9,10);

foreach ($test as $value)

{

echo "$value - ";

}

?>

1-2-3-4-5-6-7-8-9-10

-


<?php

“one”, “two”, “three”);

foreach ($test as $key => $value)

{

echo "$key - $value <br> ";

}

?>

0-1

0-2

0-3


Arrays

T

T

Special Conta

။အ

T

၂။

T

T


၃။

-

$friends = array(“Ye Gyi”,”ShweMoe”,”Moelone”);

$name [0] = “Ye Gyi”

$name [1] = “ShweMoe”

$name [2] = “Moelone”


၂။

$student_score = array (“Tarnge” =>80, “ShweMoe”=>90, “Moelone”=>85);

$student_score [“Tarnge”] = 80;

$student_score [“ShweMoe”] = 90;

$student_score [“Moelone”] = 85;

T


၃။

<?php

$array1 = array (10,15,20);

$array2 = array (110,115,120);

$array3 = array (210,215,320);

$big_array = array ($array1, $array2, $array3);

echo $big_array [1] [2];

?>

Arr


။အ

။ အ

။အ

။အ

Switching Flow

PHP Conditional Statements


If Statement

sta

။ အ

if (expression)

{

/*code to execute if the statement is true*/

}

-

<?php

$user = "Tin HTun Lwin";

State


if ($user == " Tin Htun Lwin ")

{

echo "M51A creator is Tin Htun Lwin ";

}

?>

။အ

Lwin ("Tin Ht

($user == " Tin Ht

Tin Htu

T

echo (echo "M51A

creator is Tin Ht

။ အ

M51A Creato

T

If...Else Statement

If …

Tin Htun

if …


if (expression)

{

/*code to execute if the statement is true*/

}

else

{

/*code to execute if the statement is false*/

}

-

<?php

$user = "Tarnge";

if ($user == " Tin HTun Lwin ")

{


echo "M51A creator is Tin HTun Lwin ";

}

else

{

echo “M51A Admin is Tarnge”

}?>

။အ

T

" Tin Ht

T

T

Ht

T

($user ==

echo (echo "M51A creator is Tin

echo (echo “M51A Admin is Tarnge”

။အ

T

T


If…Else If Statement

If …

if(expression1)

{

/*code to execute if the statement is true*/

}

else if(expression2)

{

/*code to execute if the previous statement is false and this statement is true*/

}

else


{

/*code to execute none of the above statements are false*/

}

-

<?php

$user = "Moe Lone";

if

($user == "Tin Htun Lwin")

{

echo "M51A creator is Tin Htun Lwin";

}

else if

($user == " Tarnge ")


{

echo "M51A Admin is Tarnge";

}

else if

($user == "Moe Lone")

{

echo " M51A Admin is Moe Lone ";

}

else

{

echo "You Are M51A Member";

}?>


Switch Statement

။ အ

switch (expression )

{

case label1:

// code to be executed if expression = label1;

break;

case label2:

// code to be executed if expression = label2;

default:

Co

break;


// code to be executed

// if expression is different

// from both label1 and label2;

}

-

<?php

$m51a = "Tin Htun Lwin";

switch ($m51a)

{

case "Creator":

case "Administrator":

echo " You Are M51A Website Administrator ";

break;


case "Moderator":

case "Supervisor":

echo " You Are M51A Administrator Group Member";

break;

case "Special Member ":

case " Member ":

echo "Hello Friend, You Are Welcome To M51A ";

break;

default:

echo "Thanks for visiting! Please either login below, or click here to register";

}?>

။အ

("Tin Ht

။ အ

Tin Htun Lwin


You

Are M51A

Administrator

Gro

T

T

Function In PHP

_

။ အ


Function MyTest()

{ အ

{ အ

}အ

} အ

-

<?php

function Greeting()

{

echo "Hello Friend, You Are Welcome To M51A<br/>";

}

?>

{}

()


<?php

function Greeting()

{

echo "Hello Friend, You Are Welcome To M51A<br/>";

}

Greeting();

?>

<?php


functionGreeting()

{

echo "Welcome To Mistake 51 Area!<br/>";

}

echo "Hello Friend <br/>";

Greeting();

echo "Hello New Member <br/>";

Greeting();

?>

Parameters

-


<?php

function Greeting($Name)

{

echo "Hello! " . $Name . "<br/>";

}

Greeting("Tarnge");

Greeting("Ye` Paing Phyo");

Greeting("Moe Lone");

Greeting("Shwe Moe Hlaing");

?>


$N

Hello!

.

("

.

$Name

.

")

“”

Hello! Tarnge

Hello! Ye` Paing Phyo

Hello! Moe Lone

Hello! Shwe Moe Hlaing

,

<?php

function Greeting($Name, $Posting)

{

။ အ


echo "Hello! " . $Name . " " . $Posting . "<br/>";

}

Greeting("Tarnge,", "You Are M51A Administrator");

Greeting("Ye` Paing Phyo,", "You Are M51A Administrator");

Greeting("Moe Lone,", "You Are M51A Moderator");

Greeting("Shwe Moe Hlaing,", "You Are M51A Supervisor");

?>

Hello! Tarnge, You Are M51A Administrator

Hello! Ye` Paing Phyo, You Are M51A Administrator

Hello! Moe Lone, You Are M51A Moderator

Hello! Shwe Moe Hlaing, You Are M51A Supervisor


PHP Functions - Returning Values

-

<?php

function plus($x, $y)

{

$total = $x * $y;

return $total;

}

plus(3, 17);

echo "M51A =" . plus(3, 17);

?>

(Return Valu


M51A =51

PHP Forms and User Input (Post Method& Get Method)

T

။ အ

T

POST Method

<html>

T


<body>

<form action="welcome.php" method="post">

Name: <input type="text" name="name" />

Age: <input type="text" name="age" />

<input type="submit" />

</form>

</body>

</html>

Fi

။ အ


The $_Request Variable

<html>

<body>

Welcome

<?php

echo $_POST["name"];

?><br />

You are

<?php

echo $_POST["age"];


?>

years old.

</body>

</html>

T

welcome.p

T

Get Method

Get


-

<html>

<body>

<form action="welcome.php" method="get">

Name: <input type="text" name="name" />

Age: <input type="text" name="age" />

<input type="submit" />

</form>

</body>

</html>

S


Date …. Time Function

T

<?php

echo date("d/m/y");

?>

m-d-y အ

Supplying a Timestamp

Ti

mktime(hour, minute, second, month, day, year, daylight savings time)

-

<?php

- 16/11/10


$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));

echo "Tomorrow is ".date("m/d/y", $tomorrow);

?>

T

Include & Require

Include () Function


-

<a href="http://www.m51a.com/index.php">Home</a>

<a href="http://www.m51a.com/ tutorials.php?">Tutorials</a>

<a href="http://www.m51a.com/ knowledge.php">Knowledge</a>

။ အ

<html>

<body>

<?php include("menu.php"); ?>


<h1>Welcome to Mistake 51 Area!</h1>

<p>Some text.</p>

</body>

</html>

Require () Function

Include

Bro

Welcome to

Warning: include(menu.php) [function.include]: failed to open stream: No such file or directory in

C:\wamp\www\Testttttttttttttt\index.php on line 3


Warning: include() [function.include]: Failed opening 'menu.php' for inclusion (include_path='.;C:\php5\pear') in

C:\wamp\www\Testttttttttttttt\index.php on line 3

Welcome to Mistake 51 Area!

Some text.

include("menu.php")

<?php

-

Warning: require(menu.php) [function.require]: failed to open stream: No such file or directory in

C:\wamp\www\Testttttttttttttt\index.php on line 3

Fatal error: require() [function.require]: Failed opening required 'menu.php' (include_path='.;C:\php5\pear') in

C:\wamp\www\Testttttttttttttt\index.php on line 3


PHP File Handling

T

File Create / File Open

-

<html><body>

<?php

$file=fopen("welcome.txt","w");

?>

</body></html>


“r”

("w"

။ “r”

။ အ

err

။ အ

။အ

Read: 'r'

Read/Write: 'r+'

Write: 'w'


Write/Read: 'w+'

Append: 'a'

Append: 'a+'

Append: 'x'

Append: 'x+'


File Close

<?php

$file = fopen("test.txt","r");

fclose($file);

?>

PHP Cookies

T

Cookie

T


။ အ

TT

TT

How to create a cookie?

Setcookie (name, value, expire, path, domain);

<?php

setcookie("user", "m51a");

?>


<html>

<?php

setcookie("user", "m51a", time()+3600);

?>

<html>

<?php

$expire=to,e()+60*60*24*30;

setcookie("user", "m51a", $expire);

?>


<html>

How to Retrieve a Cookie Value?

cook

<?php

// Print a cookie

echo $_COOKIE["user"];

// A way to view all cookies

print_r($_COOKIE);


?>

Register u

<html>

<body>

<?php if (isset($_COOKIE["user"]))

echo "Welcome " . $_COOKIE["user"] . "!<br />";

else

echo "Welcome guest!<br />";

?>

</body>

</html>

How to Delete a Cookie?


<?php

// set the expiration date to one hour ago

setcookie("user", "", time()-3600);

?>

T

PHP Sessions


-

အ အ

Starting PHP Session

<?php session_start(); ?>

<html>

Storing a Session variable

<?php

session_start();


// store session data

$_SESSION['views']=1;

?>

<html>

<body>

<?php

//retrieve session data

echo "Pageviews=". $_SESSION['views'];

?>

</body>

</html>

Pageviews=

။အ


<?php

session_start();

if(isset($_SESSION['views']))

$_SESSION['views']=$_SESSION['views']+1;

else

$_SESSION['views']=1;

echo "Views=". $_SESSION['views'];

?>


Destroing a Session

<?php

unset($_SESSION['views']);

?>

session_destroy()

<?php session_destroy(); ?>

www.m51a.com

PHP:Basic

PHP

Tutorial

Tin Htun Lwin (mistake51area@gmail.com)

E-B

Basic PHP Tutorial  

Basic PHP Tutorial By Tin Htun Lwin