×
Clear all filters including search bar
Valeri Tandilashvili's PHP Notes
empty
and isset
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
The following values are considered to be false:
""
(an empty string)
0
(0 as an integer)
0.0
(0 as a float)
"0"
(0 as a string)
NULL
FALSE
array()
(an empty array)
// Example usage:
$year = 2023;
$month = 1; // May
function generateMonthArray($year, $month) {
$numDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$firstDay = date("N", strtotime("$year-$month-01")); // 1 = Monday, 7 = Sunday
$weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
$monthArray = array_fill_keys($weekdays, []);
for ($day = 1; $day <= $numDays; $day++) {
$weekday = ($firstDay + $day - 2) % 7; // Adjust to start from Monday
$monthArray[$weekdays[$weekday]][] = $day;
}
foreach ($monthArray as &$weekDays) {
if ($weekDays[0]!=1) {
array_unshift($weekDays, '');
} else {
break;
}
}
return $monthArray;
}
$result = generateMonthArray($year, $month);
// Print the result
foreach ($result as $weekday => $days) {
echo $weekday . "\t" . implode("\t", $days) . "\n";
}
printMe()
is defined after it's called but it works without errorsecho printMe();
function printMe() {
return 'Print some text';
}
Exception is conditional function
.
In this example function foo()
will not be defined until the if ($makefoo)
conditional statement gets executed$makefoo = true;
/* We can't call foo() from here
since it doesn't exist yet,
but we can call bar() */
// foo();
bar();
if ($makefoo) {
function foo()
{
echo "I don't exist until program execution reaches me.\n";
}
}
/* Now we can safely call foo()
since $makefoo evaluated to true */
if ($makefoo) foo();
function bar()
{
echo "I exist immediately upon program start.\n";
}
Functions within functions.
Function bar()
will not be defined until the function foo()
is executedfunction foo()
{
function bar()
{
echo "I don't exist until foo() is called.\n";
}
}
/* We can't call bar() yet
since it doesn't exist. */
// bar();
foo();
/* Now we can call bar(),
foo()'s processing has
made it accessible. */
bar();
Note: All functions in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versaclass Foo {
public function __call($function, $arguments)
{
return "$function has been called with arguments: " . implode(', ', $arguments);
}
// public function fireFunction($one, $two, $three) {
// return "pre-defined method";
// }
}
$foo = new Foo();
echo $foo->fireFunction(5, 47, "third argument");
__call
is invoked when the method is inaccessible (when it's not public or does not exist at all)__get
and __set
magic methods allows us to get and set any variable without having individual getter and setter methods:class Foo {
protected $properties = array();
public function __get( $key )
{
if(array_key_exists($key, $this->properties)){
return $this->properties[$key];
}
return $this->properties[ $key ];
}
public function __set( $key, $value )
{
$this->properties[ $key ] = $value;
}
}
$foo = new Foo();
$foo->name = 'David';
echo $foo->name;
In this example, we can set any variable, even if it does not exist. Every variable will be addd to properties
array$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
Without quotes:$new = htmlspecialchars("<a href='test'>Test</a>");
echo $new; // <a href='test'>Test</a>
The second parameter possible values:
ENT_COMPAT
- Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES
- Will convert both double and single quotes.
ENT_NOQUOTES
- Will leave both double and single quotes unconverted.
The default is ENT_COMPATfile_put_contents($filePath, $content, FILE_APPEND);
Note: if FILE_APPEND
parameter is not passed, the function overwrites the content.class A {
final public static function who() {
echo __CLASS__;
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::who();
Fatal error: Cannot override final method A::who()
Animal
abstract class and both implement the same method prey()
differentlyabstract class Animal {
// child classes must implement this
abstract function prey();
public function run() {
echo "I am running!\n";
}
}
class Dog extends Animal {
public function prey() {
echo "I killed the cat !\n";
}
}
class Cat extends Animal {
public function prey() {
echo "I killed the rat !\n";
}
}
$dog = new Dog();
$cat = new Cat();
$dog->prey(); // I killed the cat !
$cat->prey(); // I killed the rat !
$dog->run(); // I am running!
$cat->run(); // I am running!
parent
keywordparent::__construct($name);
Complete example:class User {
public $username;
function __construct($name) {
$this->username = $name;
}
}
class Admin extends User{
public $admin_level;
function __construct($name, $admin_level) {
parent::__construct($name);
$this->admin_level = $admin_level;
}
}
$admin1 = new Admin('Tom', 'Manager');
echo $admin1->username;
echo "\n";
echo $admin1->admin_level;
If there is no constructor in child class, then parent class constructor will be called (when appropriate parameters are passed)