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 ျဖစ္သည့္အတြက္ေၾကာင့္ ျဖစ္ပါသည္။
။
<?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
အ