Cybercrux

Everything is achievable through technology

PHP

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language

 

Demo code

eg. 1

<!DOCTYPE html>
<meta charset="utf-8">
<title>PHP Test</title>
<?php
 echo 'Hello World';
?>

eg.2
function myFunction()  // defines a function, this one is named "myFunction"
{
    return 'John Doe'; // returns the value 'John Doe'
}

echo 'My name is ' . myFunction() . '!'; //outputs the text concatenated with the return value of myFunction.

// myFunction() is called as the result of this syntax.
// The result of the output will be 'My name is John Doe!'

eg.3
function getAdder($x)
{
    return function($y) use ($x)
           {
               return $x + $y;
           };
}

$adder = getAdder(8);
echo $adder(2); // prints "10"

eg.4

function lock()
{
    $file = fopen('file.txt', 'r+');

retry:
    if (!flock($file, LOCK_EX | LOCK_NB))
        goto retry;

    fwrite($file, 'Success!');
    fclose($file);
}

eg.5

class Person {
 public $firstName;
 public $lastName;

 public function __construct($firstName, $lastName = '') { //Optional parameter
  $this->firstName = $firstName;
  $this->lastName = $lastName;
 }

 public function greet() {
  return "Hello, my name is " . $this->firstName . " " . $this->lastName . ".";
 }

 public static function staticGreet($firstName, $lastName) {
  return "Hello, my name is " . $firstName . " " . $lastName . ".";
 }
}

$he = new Person('John', 'Smith');
$she = new Person('Sally', 'Davis');
$other = new Person('iAmine');

echo $he->greet(); // prints "Hello, my name is John Smith."
echo '<br />';
echo $she->greet(); // prints "Hello, my name is Sally Davis."
echo '<br />';
echo $other->greet(); // prints "Hello, my name is iAmine ."
echo '<br />';
echo Person::staticGreet('Jane', 'Doe'); // prints "Hello, my name is Jane Doe."



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s