TUTORIAL PHP



















The ternary operator
The switch statement
Using if, else, and else if together
The else-if statement
The else statement
The if statement
PHP's built-in functions
Returning values
Calling functions
Using parameters
Creating functions
Printing variables
Example of declaring a variable and then giving it a value
Example of initializing a variable
Including comments in a script
Including HTML tags in a script
Printing text on a webpage
Declaring a script in an external file
Declaring a script within a webpage 
Cara install PHP


GALERIGILA GALERIGILA GALERIGILA GALERIGILA GALERIGILA

The ternary operator
 
<?php 
$x; 
$y = 9; 
 $x = ($y==4) ? 5: 15; 
print "x = " . $x; 
?>
Output:
x = 15


The switch statement

<?php 
$X = 7;  
switch($X){ 
case 1: 
print "X is equal to 1"; 
break; 
case 2: 
print "X is equal to 2"; 
break; 
case 3: 
print "X is equal to 3";
break; 
case 7: 
print "X is equal to 7"; 
break; 
default: 
print "X is not equal to any of the values specified"; 
} 
?>
Output:
X is equal to 7
 


Using if, else, and else if together
<?php $X = 5; 
 if($X == 9){ 
print "X is equal to 9"; 
else if($X == 7){ 
print "X is equal to 7"; 
 else if($X == 3){ 
print "X is equal to 3"; 
} else if($X == 15){ 
print "X is equal to 15"; 
else
print "X is not equal to 9, 7, 3, or 15. X is equal to " . $X; 
} 
?>
Output:
X is not equal to 9. 7, 3, or 15. X is equal to 5
 
 
<?php $X = 5; 
$Y = 7; 
 if($X == 9){ 
print "X is equal to 9"; 
print "Y is equal to 7"; } 
else if($X == 7) 
print "X is equal to 7"; 
else if($X == 3){ 
print "X is equal to 3";
 print "Y is equal to 7"; 
else if($X == 15){ 
print "X is equal to 15"; 
print "Y is equal to 7"; 
else 
print "X is not equal to 9, 7, 3, or 15, it is equal to 5"; 
?>



The else-if statement
<?php $X = 7;  
if($X == 5){ 
print "X is equal to 5"; 
else 
if($X == 7) { 
print "X is equal to 7"; 
} 
?>
Output:
X is equal to 7
 

The else statement
<?php $plant = "cactus";  
if ($plant == "cactus"){ 
print "Great choice!";
else
{
print "Still a good choice, though cactuses are better."; 
} 
?>
Output:
Great Choice! 
 
<?php $plant = "birch"; 
 if ($plant == "cactus"){ 
print "Great choice!"; 
else
print "Still a good choice, though cactuses are better."; 
} 
?>
Output:
Still a good choice, though cactuses are better.
 

The if statement
<?php 
$aNumber = 5; 
if ($aNumber == 5){ print "aNumber is equal to 5"; 
} 
?>
Output:
aNumber is equal to 5
 

PHP's built-in functions
PHP has several built-in functions you can use for various purposes.
  • abs() - Will return the absolute value of a number.
  • max() - Returns the highest value from a set of specified numbers.
  • strrev() - Will print a text string backwards.
  • strtoupper() - Will print a text string in all uppercase letters.
Example:
<?php $a = abs(-5); 
$highest = max(5, 10, 15, 20); 
$text = strrev("This is a text string."); 
$textInUpperCase = strtoupper("here is some text. this is the second sentence of the text."); 
 print $a; 
print "<br />" . $highest; print "<br />" . $text; print "<br />" . $textInUpperCase; 
?>
Output:
5
20
.gnirts txet a si sihT
HERE IS SOME TEXT. THIS IS THE SECOND SENTENCE OF THE TEXT.
Returning values
<?php 
//return the addition of two numbers //supplied by the parameters 
function add($num1, $num2){
return $num1 + $num2;
//add 2 and 4 and put the value in $additionResult 
$additionResult = add(2, 4);
//print the value of $additionResult 
print "2 + 4 = " . $additionResult; //skip a line print "<br />"; 
//print the value returned by passing 
//the parameters 10 and 10 to the add() function 
print "10 + 10 = " . add(10, 10); 
?>
Output:
2 + 4 = 6
10 + 10 = 20

Calling functions
<?php
function subtract($num1, $num2){
print $num1 - $num2; }
 subtract(10, 5);
?>

Using parameters
<?php
function printMessage($name){
print "Hello " . $name . "!";
}
?> 

Creating functions
<?php
function printMessage(){
print "Hello, this is a function.";
}
?>

Printing variables

<?php 
$color = "blue"; 
$numChairs = 5;  
print $color; 
echo "<br />$numChairs"; 
?>
Output:
blue
5
<?php 
$teamName = "The Penguins"; 
$teamState = "Connecticut"; 
$numWins = 12; 
$numLosses = 2;  
print "The name of the team is " . $teamName; 
print "<br />The team is from " . $teamState; 
print "<br />" . $teamName . " have won " . $numWins . " games and have lost " . $numLosses . " games"); ?>
Output:
The name of the team is The Penguins
The team is from Connecticut
The Penguins have won 12 games and have lost 2 games



Example of declaring a variable and then giving it a value
<?php
$penColor;
$penColor = "blue";
?>

<?php
//declare a text variable and assign a value to it
$penColor = "blue";
 //declare a numeric variable and assign a value to it
$age = 20; ?>

Example of initializing a variable
<?
php $penColor = "blue";
?> 

Including comments in a script

<html>
<head>
<title>
PHPcomments
</title>
</head>
<body>
<?php
//thisisasinglelinecomment
/*hereisamultilinecommentitwillspanmultiplelinesthisisthethirdlineofthemultilinecomment
*/
echo"Hereissometext";
?>
</body>
</html>

GALERIGILA GALERIGILA GALERIGILA GALERIGILA GALERIGILA

Including HTML tags in a script  

<html>
<head>
<title>
HTMLtagsinscripts
</title>
</head>
<body>
<?php
print"<b><i>Thistextwillbeboldanditalic</i></b>";
echo"<br/><ahref='http://www.landofcode.com'>Landofcode.commainpage</a>";?>
</body>
</html>

Output:
This text will be bold and italic
Landofcode.com main page
GALERIGILA GALERIGILA GALERIGILA GALERIGILA GALERIGILA

Printing text on a webpage
<html>
<head>
<title>
Printandechocommands
</title>
</head>
<body>
<?
phpprint"Hereissometext.";
echo"Hereissomemoretext.";
?>
</body>
</html>

Output:
Here is some text. Here is some more text.


GALERIGILA GALERIGILA GALERIGILA GALERIGILA GALERIGILA

Declaring a script in an external file 
<html>
<head>
<title>
Externalscripts
</title>
</head>
<body>
<?
phpinclude("script1.php");
?>
</body>
</html>
GALERIGILA GALERIGILA GALERIGILA GALERIGILA GALERIGILA

Declaring a script within a webpage

<html>
<head>
<title>PHPscript</title>
</head>
<body>
<?phpprint"PHP is cool!";?>
</body>
</html>

Output:
PHP is cool!

GALERIGILA GALERIGILA GALERIGILA GALERIGILA GALERIGILA

Cara install PHP

Gunakan appserv
Appserv merupakan sebuah paket untuk Windows dimana didalamnya terdapat :
Apache Web Server
PHP Script Language
MySQL Database
Zend Optimizer
phpMyAdmin Database Manager


buka http://www.AppServNetwork.com
download Appserv
jalankan filenya
lalu ada konfirmasi, klik Next


default folder disarankan tidak usah diubah
Server Name default : localhost
Port default nya 80

GALERIGILA GALERIGILA GALERIGILA GALERIGILA GALERIGILA



Related Search