Thursday, 10 September 2015

Web develpment PHP programming






Example:1
<!DOCTYPE html>
<html>
<body>

<?php
echo "My first PHP script!";
?>
  

</body>
</html>
Example:2
<!DOCTYPE html>
<html>
<body>

<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;

echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>


</body>
</html>
Example:3
<!DOCTYPE html>
<html>
<body>

<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>


</body>
</html>
Example :4
<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;
$y = 4;
echo $x + $y;
?>


</body>
</html>
Example:5
<!DOCTYPE html>
<html>
<body>

<?php
$x = 5; // global scope
  
function myTest() {
     // using x inside this function will generate an error
     echo "<p>Variable x inside function is: $x</p>";
} 
myTest();

echo "<p>Variable x outside function is: $x</p>";
?>


</body>
</html>
Example:6
<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
     $x = 5; // local scope
     echo "<p>Variable x inside function is: $x</p>";
} 
myTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>


</body>
</html>
Example:7
<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;
$y = 10;

function myTest() {
     global $x, $y;
     $y = $x + $y;
} 

myTest(); // run function
echo $y; // output the new value for variable $y
?>


</body>
</html>
Example:8
<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;
$y = 10;

function myTest() {
     $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
} 

myTest();
echo $y;
?>


</body>
</html>
Example:9
<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;
$y = 10;

function myTest() {
     $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
} 

myTest();
echo $y;
?>


</body>
</html>
Example:10
<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
     static $x = 0;
     echo $x;
     $x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
  

</body>
</html>
Example:11
<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
     static $x = 0;
     echo $x;
     $x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
  

</body>
</html>
Example:12
<!DOCTYPE html>
<html>
<body>

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
  

</body>
</html>
Example:13
<!DOCTYPE html>
<html>
<body>

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>


</body>
</html>
Example:13
<!DOCTYPE html>
<html>
<body>

<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
  

</body>
</html>
Example:14

<!DOCTYPE html>
<html>
<body>

<?php 
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>"; 
echo $y;
?>


</body>
</html>
Example:15
<!DOCTYPE html>
<html>
<body>

<?php 
$x = 5985;
var_dump($x);
?>
   

</body>
</html>
Example:16
<!DOCTYPE html>
<html>
<body>

<?php 
$x = 5985;
var_dump($x);
?>
   

</body>
</html>
Example:17
<!DOCTYPE html>
<html>
<body>

<?php 
$x = 10.365;
var_dump($x);
?>
   

</body>
</html>
Example:18
<!DOCTYPE html>
<html>
<body>

<?php 
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
   

</body>
</html>
Example:19
<!DOCTYPE html>
<html>
<body>

<?php
class Car {
     function Car() {
         $this->model = "VW";
     }
}
// create an object
$herbie = new Car();

// show object properties
echo $herbie->model;
?>


</body>
</html>
Example:20
<!DOCTYPE html>
<html>
<body>

<?php
class Car {
     function Car() {
         $this->model = "VW";
     }
}
// create an object
$herbie = new Car();

// show object properties
echo $herbie->model;
?>


</body>
</html>
Example:21

<!DOCTYPE html>
<html>
<body>

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>


</body>
</html>
Example:22
<!DOCTYPE html>
<html>
<body>

<?php
echo strlen("Hello world!");
?>
  
  
</body>
</html>
Example:23
<!DOCTYPE html>
<html>
<body>

<?php
echo str_word_count("Hello world!");
?>
  
  
</body>
</html>
Example:24
<!DOCTYPE html>
<html>
<body>

<?php
echo strrev("Hello world!");
?>
  
  
</body>
</html>
Example:25

<!DOCTYPE html>
<html>
<body>

<?php
echo strpos("Hello world!", "world");
?>
  
  
</body>
</html>
Example:26
<!DOCTYPE html>
<html>
<body>

<?php
echo str_replace("world", "Dolly", "Hello world!");
?>
  
  
</body>
</html>
Example:27
Chapter 2

                  LOOP
While LOOP
Example:1
<!DOCTYPE html>
<html>
<body>

<?php 
$x = 1;
  
while($x <= 5) {
   echo "The number is: $x <br>";
   $x++;
} 
?>
   

</body>
</html>
Example:2
<!DOCTYPE html>
<html>
<body>

<?php 
$x = 1;

do {
     echo "The number is: $x <br>";
     $x++;
} while ($x <= 5);
?>


</body>
</html>
Example:3
<!DOCTYPE html>
<html>
<body>

<?php 
$x = 6;

do {
     echo "The number is: $x <br>";
     $x++;
} while ($x <= 5);
?>


</body>
</html>
For LOOP
Example:4
<!DOCTYPE html>
<html>
<body>

<?php 
for ($x = 0; $x <= 10; $x++) {
   echo "The number is: $x <br>";
}
?>
   

</body>
</html>
Example:5
<!DOCTYPE html>
<html>
<body>

<?php 
$colors = array("red", "green", "blue", "yellow"); 

foreach ($colors as $value) {
   echo "$value <br>";
}
?>
   

</body>
</html>

PHP Function

Example:6

<!DOCTYPE html>
<html>
<body>

<?php
function writeMsg() {
     echo "Hello world!";
}

writeMsg();
?>


</body>
</html>
Example:7
<!DOCTYPE html>
<html>
<body>

<?php
function familyName($fname) {
     echo "$fname Refsnes.<br>";
}

familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>


</body>
</html>
Example:8
<!DOCTYPE html>
<html>
<body>

<?php
function familyName($fname, $year) {
     echo "$fname Refsnes. Born in $year <br>";
}

familyName("Hege","1975");
familyName("Stale","1978");
familyName("Kai Jim","1983");
?>


</body>
</html>
Example:9
<!DOCTYPE html>
<html>
<body>

<?php
function setHeight($minheight = 50) {
     echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>


</body>
</html>
Example:10
<!DOCTYPE html>
<html>
<body>

<?php
function sum($x, $y) {
     $z = $x + $y;
     return $z;
}

echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>


</body>
</html>
Example:11

ARRAY
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota"); 
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>


</body>
</html>
Example:2
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota"); 
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>


</body>
</html>
Example:3
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>


</body>
</html>
Example:4
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);

for($x = 0; $x <  $arrlength; $x++) {
     echo $cars[$x];
     echo "<br>";
}
?>


</body>
</html>
Example:5

<!DOCTYPE html>
<html>
<body>

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>


</body>
</html>
Example:6

<!DOCTYPE html>
<html>
<body>

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {
     echo "Key=" . $x . ", Value=" . $x_value;
     echo "<br>";
}
?>


</body>
</html>
Example:7
<!DOCTYPE html>
<html>
<body>

<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);

$arrlength = count($numbers);
for($x = 0; $x <  $arrlength; $x++) {
     echo $numbers[$x];
     echo "<br>";
}
?>


</body>
</html>
Example:8

<!DOCTYPE html>
<html>
<body>

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);

$arrlength = count($numbers);
for($x = 0; $x <  $arrlength; $x++) {
     echo $numbers[$x];
     echo "<br>";
}
?>


</body>
</html>
Example:9
<!DOCTYPE html>
<html>
<body>

<?php 
$x = 75;
$y = 25; 

function addition() {
     $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

addition();
echo $z;
?>


</body>
</html>
Example:10
<!DOCTYPE html>
<html>
<body>

<form method="post" action="
<?php echo $_SERVER['PHP_SELF'];?>">
   Name: <input type="text" name="fname">
   <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
     // collect value of input field
     $name = $_REQUEST['fname']; 
     if (empty($name)) {
         echo "Name is empty";
     } else {
         echo $name;
     }
}
?>


</body>
</html>
Example:12

<!DOCTYPE html>
<html>
<body>

<form method="post" action="
<?php echo $_SERVER['PHP_SELF'];?>">
   Name: <input type="text" name="fname">
   <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
     // collect value of input field
     $name = $_POST['fname']; 
     if (empty($name)) {
         echo "Name is empty";
     } else {
         echo $name;
     }
}
?>


</body>
</html>
Example:13


No comments:

Post a Comment