PHP LAB

 

GLOBAL AND LOCAL SCOPE IMPLEMENTATION BY PHP PROGRAM

Variable with global scope:

<html>

<body>

<?php

$x = 5;

 function myTest()

 {

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

}

myTest();

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

?>

 

</body>

</html>

 

Output:

Variable x inside function is:

Variable x outside function is: 5

Variable with local scope:

<html>

<body>

 

<?php

function myTest()

{

  $x = 5;

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

}

myTest();

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

?>

 

</body>

</html>

o/p: Variable x inside function is: 5

Variable x outside function is:

PHP OBJECT IMPLEMENTATION

<html>

<body>

 

<?php

class Car {

  public $color;

  public $model;

  public function __construct($color, $model) {

    $this->color = $color;

    $this->model = $model;

  }

  public function message () {

    return "My car is a " . $this->color . " " . $this->model . "!";

  }

}

$myCar = new Car("black", "Volvo");

echo $myCar -> message();

echo "<br>";

$myCar = new Car("red", "Toyota");

echo $myCar -> message();

?>

</body>

</html>

O/p:  My car is a black Volvo!

My car is a red Toyota!

ABSTRACT METHOD USING PHP

<!DOCTYPE html>

<html>

<body>

 

<?php

abstract class ParentClass {

  // Abstract method with an argument

  abstract protected function prefixName($name);

}

class ChildClass extends ParentClass {

  public function prefixName($name) {

    if ($name == "John Doe") {

      $prefix = "Mr.";

    } elseif ($name == "Jane Doe") {

      $prefix = "Mrs.";

    } else {

      $prefix = "";

    }

    return "{$prefix} {$name}";

  }}

$class = new ChildClass;

echo $class->prefixName("John Doe");

echo "<br>";

echo $class->prefixName("Jane Doe");

?>

</body></html>


ABSTRACT CLASS IN PHP

 

<!DOCTYPE html>

<html>

<body>

 

<?php

// Parent class

abstract class Car {

  public $name;

  public function __construct($name) {

    $this->name = $name;

  }

  abstract public function intro() : string;

}

 

// Child classes

class Audi extends Car {

  public function intro() : string {

    return "Choose German quality! I'm an $this->name!";

  }

}

 

class Volvo extends Car {

  public function intro() : string {

    return "Proud to be Swedish! I'm a $this->name!";

  }

}

 

class Citroen extends Car {

  public function intro() : string {

    return "French extravagance! I'm a $this->name!";

  }

}

 

// Create objects from the child classes

$audi = new audi("Audi");

echo $audi->intro();

echo "<br>";

 

$volvo = new volvo("Volvo");

echo $volvo->intro();

echo "<br>";

 

$citroen = new citroen("Citroen");

echo $citroen->intro();

?>

 

</body>

</html>


INHERITANCE IMPLEMENTATION

<html>

<body>

 

<?php

class Fruit {

  public $name;

  public $color;

  public function __construct ($name, $color) {

    $this->name = $name;

    $this->color = $color;

  }

  public function intro () {

    echo "The fruit is {$this->name} and the color is {$this->color}.";

  }

}

 

// Strawberry is inherited from Fruit

class Strawberry extends Fruit {

  public function message () {

    echo "Am I a fruit or a berry? ";

  }

}

 

$strawberry = new Strawberry ("Strawberry", "red");

$strawberry->message ();

$strawberry->intro ();

?>

 

</body>

</html>

Am I a fruit or a berry? The fruit is Strawberry and the color is red.


PHP - Overriding Inherited Methods

<html>

<body>

 

<?php

class Fruit {

  public $name;

  public $color;

  public function __construct($name, $color) {

    $this->name = $name;

    $this->color = $color;

  }

  public function intro() {

    echo "The fruit is {$this->name} and the color is {$this->color}.";

  }

}

class Strawberry extends Fruit {

  public $weight;

  public function __construct($name, $color, $weight) {

    $this->name = $name;

    $this->color = $color;

    $this->weight = $weight;

  }

  public function intro() {

    echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";

  }

}

$strawberry = new Strawberry("Strawberry", "red", 50);

$strawberry->intro();

?>

 

</body>

</html>

 

The fruit is Strawberry, the color is red, and the weight is 50 gram.


associative array using for each() loop

<?php

 

// Declare an associative array

$aso_arr = array(

    "Up"=>"North",

    "Down"=>"South",

    "Left"=>"West",

    "Right"=>"East"

);

 

// Use foreach loop to traverse each

// elements of array and display its

// key and value

foreach($aso_arr as $side=>$direc) {

    echo $side . " => " . $direc . "\n";

}

 

?>

o/P: Up => North Down => South Left => West Right => East


prime numbers between 1 to 100.

<?php 

            $limit = 100;

            $init = 2;

            while(TRUE)

            {

                        $div = 2;

                        if($init > $limit)

                        {

                                    break;

                        }

                        while(TRUE)

                        {

                                    if($div > sqrt($init))

                                    {

                                                echo $init."  ";

                                                break;

                                    }

                                    if($init % $div == 0)

                                    {

                                                break;

                                    }

                                    $div = $div + 1;

                        }

                        $init = $init + 1;

            }

?>

Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97


<!DOCTYPE HTML> 

<html>

<head>

</head>

<body> 

 

<?php

// define variables and set to empty values

$name = $email = $gender = $comment = $website = "";

 

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  $name = test_input($_POST["name"]);

  $email = test_input($_POST["email"]);

  $website = test_input($_POST["website"]);

  $comment = test_input($_POST["comment"]);

  $gender = test_input($_POST["gender"]);

}

function test_input($data) {

  $data = trim($data);

  $data = stripslashes($data);

  $data = htmlspecialchars($data);

  return $data;

}

?>

<h2>PHP Form Validation Example</h2>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

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

  <br><br>

  E-mail: <input type="text" name="email">

  <br><br>

  Website: <input type="text" name="website">

  <br><br>

  Comment: <textarea name="comment" rows="5" cols="40"></textarea>

  <br><br>

  Gender:

  <input type="radio" name="gender" value="female">Female

  <input type="radio" name="gender" value="male">Male

  <input type="radio" name="gender" value="other">Other

  <br><br>

  <input type="submit" name="submit" value="Submit"> 

</form>

 

<?php

echo "<h2>Your Input:</h2>";

echo $name;

echo "<br>";

echo $email;

echo "<br>";

echo $website;

echo "<br>";

echo $comment;

echo "<br>";

echo $gender;

?>

</body>

</html>

count no of words in a given string

<?php 

 $str="PHP is nice"; 

 echo "Your string is:".$str; 

 echo "<br>"; 

echo "By using str_word_count(): ".str_word_count($str); 

?> 

 

Your string is:PHP is nice

By using str_word_count(): 3 


Post a Comment

Post a Comment (0)

Previous Post Next Post