PHP

What the PHP?

Almost Web purpose only

Easy to use / fast development

Slower than compiled languages

Syntax: Where to start

#include <iostream>
using namespace std;

int main() 
{
    cout << "Hello!" << endl;
    return 0;
}
<?php
echo "Hello!";
print "Hello!"

C++

PHP

Python

PHP, a scripting language!

<h1>Hello</h1>
     The time is <?php echo date('Y-m-d H:i:s');   ?>.
<hr>
This is a PHP hypertext processed page.

... but Could be written entirely in php

<?php
echo "Hello, world!\n";
echo 'The time is now ', date('Y-m-d H:i:s'), ".\n";

Syntax: Variables

int intVar = 5;
float floatVar = 1.5;
const char* text = "hello world!";
<?php

$intVar = 5;
$floatVar = 1.5;
$text = "hello world!";

// constant variable
define('gravity', 10);

echo gravity;
intVar = 5
floatVar = 1.5
text = "hello world!"

C++

PHP

Python

Syntax: Strings

 // These two statements are equivalent:
 $x = 5;
 $a = "Apples are $x each.";
 $a = 'Apples are ' . $x . ' each.';

 $test = 5;
 $a = 'test';
 $$a = 3;     // changes $test to 3 , equivalent to $test = 3

 echo '3' == 3; // true
 echo '3' === 3; // false

Syntax: if

int a = 5;
if (a == 5)
{
    // statements
}
else if (condition2)
{
    // statements
}
else
{
    // statements
}
$a = 5;
if ($a == 5) {
    // statements
} elseif (condition2) {
    // statements
} else {
    // statements
}
a = 5
if a == 5:
    # statements
elif condition2:
    # statements
else:
    # statements

C++

PHP

Python

Syntax: loops

while (condition)
{
    // statements
}


do {
    // statements
} while (condition);


for (int i = 0; i < 10; i++)
{
    // statements
}

for (string element : list)
{
    // statements
}
while (condition){
    // statements
}


do {
    // statements
} 
while (condition);


for ($i = 0; $i < 10; $i++) {
    // statements
}

foreach ($list as $element) {
    // statements
}
while condition:
    # statements


# no do-while


for i in xrange(10):
    # statements


for element in lst:
    # statements

C++

PHP

Python

Syntax: array

int a[10];
a[0] = 1;

int* b = new int[n];
b[n-1] = 1;

vector<int> dynamic;
dynamic.push_back(1);

map<string, int> aMap;
aMap["ali"] = 19;

$lst = [0, 2, 3, "text", 1.5];
$lst[1] = 13;

$lst = [];
$lst[] = 0;
$lst[] = 1;


$map = ["key" => "value"];
$map["Ali"] = 19;
lst = [1, 2, 3, "text", 1.5]
lst[0] = 13


lst = []
lst.append("text1");
lst.append("text2");


dict = { "key": "Value" }
dict["ali"] = 19

C++

PHP

Python

Syntax: functions

void setItem(int item)
{
    // ...
}

int getItem()
{
    return 5;
}
function setItem($item) {
    // ...
}


function getItem() {
    return 5;
}
def set_item(item):
    # ...


def get_item():
    return 5

C++

PHP

Python

Syntax: OO

class Vector : public Point
{
public:
    // ...
    int getX();

private:
    int x = 0;
    int y = 0;
}

Vector::getX()
{
    return x;
}

int main()
{
    Vector* v = new Vector(...);
    v->getX();

    // ...
}
class Vector extends Point {
    private $x = 0;
    private $y = 0;
    
    // ..

    public function getX() {
        return $this->x;
    }
}


$v = new Vector(...)
$v->getX();
class Vector(Point):
    def __init__(self, ...):
        self.x = 0
        self.y = 0

    def get_x(self):
        return self.x


v = Vector(...)
v.get_x()

C++

PHP

Python

How to run?

$ g++ source.cpp && ./a.out
$ php source.php
$ python source.py

C++

PHP

Python

How to run?

g++ source.cpp && ./a.out
php source.php
python source.py

C++

PHP

Python

Run via WebServer

HTTP requests: $_GET

foreach ($_GET as $key => $value) {
    echo $key . ': ' . $value;
    echo "\n";
}

// output:
// key1: value1
// key2: value2
// key3: value3

HTTP requests: $_POST

foreach ($_POST as $key => $value) {
    echo $key . ': ' . $value;
    echo "\n";
}

// output:
// pk1: value1
// pk2: value2
// pk3: value3

deck

By Amirhossein Kazemnejad