×
Clear all filters including search bar
Valeri Tandilashvili's PHP Notes
function printGreeting() {
print "Prints: Hello world!\n";
}
printGreeting();
Function that returns something
function returnGreeting() {
return "Returns: Hello world!\n";
}
echo returnGreeting();
Parameterized function that doubles the number
function doubleMe($number) {
$result = "$number times 2 is: " . $number * 2 . "\n";
return $result;
}
echo doubleMe(4);
Function calculates the sum of two numbers
function add($num1, $num2) {
$sum = $num1 + $num2;
$result = "Sum of $num1 and $num2 is: $sum";
return $result;
}
echo add(4, 5);$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} else{
echo "Have a nice day!";
}
echo "\n";
Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
If $age is more than 18, prints Adult otherwise prints Child
$age = 20;
if ($age > 18) {
echo 'Adult';
} else {
echo 'Child';
}strlen() returns the length of the string
echo strlen("Hello world!")."\n"; // 12
Function strrev() returns the Reverse of the string
echo strrev("Hello world!")."\n"; // !dlrow olleH
Function strpos() tries to find the second parameter and returns its index
echo strpos("Hello world!", "world")."\n"; // 6
Function str_replace() replaces the word "Hello" with "Hi"
echo str_replace("Hello", "Hi", "Hello there")."\n"; // Hi there
Function substr() returns the substring between given indexes
echo substr("Hello world", 6)."\n"; // world
echo substr("Hello world", 6, 3)."\n"; // wor
echo substr("Hello world", -5)."\n"; // world
echo substr("Hello world", -4, 2)."\n"; // or
Function trim() removes whitespaces from sides of the string
echo trim(" Hello World ").".\n"; // Hello World.
// Removes whitespaces from the right end
echo rtrim(" Hello World ")."\n"; // Hello World
// Removes Exclamation mark from the right end
echo rtrim("Hello World!", "!")."\n"; // Hello World
Function strtoupper() converts the string to upper case
echo strtoupper("Hello World")."\n";
Function strtolower() converts the string to lower case
echo strtolower("Hello World")."\n";
Function explode() breaks the string by its delimiter
print_r (explode(" ", "It's the first day of the course!"));
Function implode() joins the array element based on the delimiter and returns the string
echo implode(" ", ['Hello', 'World']);$variable, $Variable and $VARIABLE are completely different// lowercase
$variable = "Value 1";
// PascalCase
$Variable = "Value 2";
// UPPERCASE
$VARIABLE = "Value 3";
echo $variable . "\n";
echo $Variable . "\n";
echo $VARIABLE . "\n";
Function names are case-insensitive
function printGreeting() {
print "Prints: Hello world!\n";
}
printGreeting();Calling the function with uppercase will still worklogError function and passes details of the error, such as:
file, line, code, message and trace try {
undefinedFunction();
} catch (Error $e) {
logError(
'ERR FILE: ' . $e->getFile() . "\n" .
'ERR LINE: ' . $e->getLine() . "\n" .
'ERR CODE: ' . $e->getCode() . "\n" .
'ERR TEXT: ' . $e->getMessage() . "\n" .
'ERR TRACE String: ' . $e->getTraceAsString()
);
}
The function logError appends current date and time to the error details and logs into the error filefunction logError($error) {
$content = "\n\n\n".date('Y-m-d H:i:s');
$content .= "\n".$error;
file_put_contents('/var/log/errors.txt', $content, FILE_APPEND);
}int parameter.
If any other type is passed, fatal error will be generatedclass Book {
public $price;
public function price(int $price) {
$this->price = $price;
}
}
$book = new Book;
$book->price('k34');
echo $book->price;
Fatal error: Uncaught TypeError: Argument 1 passed to Book::price() must be of the type int, string givenclass Foo {
public function __toString()
{
return "Some text about the OBJECT";
}
}
$foo = new Foo();
echo $foo;
__toString method gets invoked when we echo or print the objectinterface Talkative {
public function talk();
}
class Cat implements Talkative {
public function talk() {
return 'Woof' . PHP_EOL;
}
}
class Dog implements Talkative {
public function talk() {
return 'Meow' . PHP_EOL;
}
}
class Tortoise implements Talkative {
public function talk() {
return 'Yak yak yak yak ...' . PHP_EOL;
}
}
$cat = new Cat;
$dog = new Dog;
$tortoise = new Tortoise;
echo $cat->talk();
echo $dog->talk();
echo $tortoise->talk();class Bird{
public $canFly;
public $legCount;
public $className;
public function __construct($canFly, $legCount, $className) {
$this->canFly = $canFly;
$this->legCount = $legCount;
$this->className = $className;
}
public function canFly() {
return $this->canFly;
}
public function getLegCount() {
return $this->legCount;
}
public function getBirdInfo() {
$canFly = $this->canFly() ? 'can fly' : 'can not fly';
return $this->className. ' ' . $canFly . ' and has ' . $this->getLegCount() . ' legs' . PHP_EOL;
}
}
class Pigeon extends Bird{
public function __construct($canFly, $legCount) {
parent::__construct($canFly, $legCount, get_class());
}
}
class Penguin extends Bird{
public function __construct($canFly, $legCount) {
parent::__construct($canFly, $legCount, get_class());
}
}
$pigeon = new Pigeon(true, 2);
$penguin = new Penguin(false, 2);
echo $pigeon->getBirdInfo();
echo $penguin->getBirdInfo();$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}