php_basics
php_basics
Introduction to PHP
2. Conditional Constructs
php
Copy code
if ($a > $b) {
echo "A is greater.";
} else {
echo "B is greater.";
}
php
Copy code
switch ($day) {
case "Monday":
echo "Start of the week.";
break;
default:
echo "Other days.";
}
3. Loops
php
Copy code
for ($i = 0; $i < 5; $i++) {
echo $i;
}
php
Copy code
foreach ($array as $value) {
echo $value;
}
4. Functions
php
Copy code
function sayHello() {
echo "Hello!";
}
sayHello();
php
Copy code
function add($a, $b) {
return $a + $b;
}
echo add(2, 3);
5. Arrays
php
Copy code
$arr = ["Apple", "Banana", "Cherry"];
php
Copy code
$arr = ["name" => "John", "age" => 25];
o Multidimensional Array: Contains nested arrays.
php
Copy code
$arr = [
["John", 25],
["Jane", 30]
];
6. Super Globals
Super global variables are built-in variables that are always accessible, regardless of
scope.
Examples:
o $_GET: Captures data sent via URL query strings.
o $_POST: Captures data sent via HTML forms.
o $_SESSION & $_COOKIE: Handle session and cookie data.
o $_SERVER: Provides information about the server and request.
7. Object-Oriented PHP
php
Copy code
class Car {
public $color;
public function __construct($color) {
$this->color = $color;
}
}
$car = new Car("Red");
echo $car->color;
o Inheritance: Classes can inherit properties and methods from other classes.
o Encapsulation: Controls access to data using public, private, and protected
modifiers.
o Polymorphism: Allows methods to behave differently in derived classes.
In Summary:
These topics provide the foundation for PHP programming, allowing developers to write
dynamic, interactive, and structured code. Let me know if you'd like examples or deeper
explanations for any section!