Refer to Resource Wiki
All PHP code must be contained within <?php and ?>
Every statement has to be terminated with a semicolon.
<?php
// code goes here
?>
Save the file with php extension.
Demonstration time.
If the URL points to a directory rather than a file, the server will serve a default page index.php within that directory.
<?php
$a = 10; //Assigns 10 to $a.
$b = 5; //Assigns 5 to $b.
$c = $a + $b;
echo $c; //Prints value of $c.
?>
<?php
$a = "Hello World"; //String Assignment
echo $a; //Prints value of $a.
?>
<?php
$a = 5;
echo $a;
$b = "hello world";
echo $b;
?>
<?php
echo 5;
echo "hello world";
?>
<?php
echo 5+15;
echo 25*5;
?>
Concatenation means connecting/joining things
<?php
$a = 'hello';
$b = 'world';
$c = $a . $b;
// prints helloworld to screen
echo $c;
// prints 12 to screen
echo 1 . 2;
?>
<?php
$a = 'hello';
$a .= ' new recruits';
// prints hello new recruits to screen
echo $a;
?>
<?php
$factorial = 1;
for($i = 1; $i <= 10; $i++) {
$factorial = $factorial * $i;
}
echo $factorial;
?>
<?php
$factorial = 1;
$i = 1;
while($i <= 10) {
$factorial = $factorial * $i;
$i++; // increment
}
// prints the value of 10!
echo $factorial;
?>
<?php
$factorial = 1;
$i = 1;
do {
$factorial = $factorial * $i;
$i++; // increment
} while($i <= 10);
// prints the value of 10!
echo $factorial;
?>
<?php
//$applications = array("Bourbon", "Muzi", "CodeVillage", "Erdős");
$applications = ["Bourbon", "Muzi", "CodeVillage", "Erdős"];
// Muzi is a music player hosted on the intranet.
echo $applications[1] . " is a music player hosted on the intranet.";
echo "Applications developed by SDSLabs: ";
foreach($applications as $application) {
echo "{$application} ";
}
?>
// Output
Applications developed by SDSLabs: Bourbon Muzi CodeVillage Erdős
Associates values to keys
$ages["Kandoi"]
<?php
$ages = array(
"Kandoi" => 19,
"Ravi" => 20
);
// Ravi is 20 years old
echo "Ravi is {$ages['Ravi']} years old.";
foreach($ages as $name => $age) {
echo "{$name} is {$age} years old.";
}
?>
Blocks of code which are used multiple times
eg: pow function computes a b
<?php
function FUNCTION_NAME(arguments) {
// code
}
?>
<?php
function sayHello($name) {
echo "Hello {$name}";
}
sayHello("new recruits");
?>
<?php
function sayHello($name) {
return "Hello {$name}";
}
echo sayHello("new recruits");
?>
<form>
First: <input type="text" name="Firstname" maxlength="3">
Last: <input type="text" name="Lastname" maxlength="5">
<input type="submit" value="SUBMIT">
</form>
<?php
class Member {
public $name = "Name";
public $age = 0;
function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function description() {
echo "{$this->name} is a member of SDSLabs.";
echo "He is {$this->age} years old.";
}
}
$parag = new Member("Parag Nandi", 19);
$parag->description();
?>
Visibility of functions can be public, protected and private.
<?php
// connecting to database
$link = new mysqli('server', 'user', 'password', 'database');
if($link->connect_errno) {
die ("Connection attempt unsuccessful!");
} else {
echo "Connection made!";
}
?>
<?php
$link = new mysqli('server', 'user', 'password', 'database');
if($link->connect_errno) {
die ("Connection attempt unsuccessful!");
} else {
echo "Connection made!";
}
$result = $link->query("SELECT * FROM users");
// do some processing on $result
?>
<?php
$link = new mysqli('server', 'user', 'password', 'database');
if($link->connect_errno) {
die ("Connection attempt unsuccessful!");
} else {
echo "Connection made!";
}
$result = $link->query("SELECT * FROM users");
$link->close();
?>