Check if string is empty

empty() function

$str = '';
if (empty($str)) {
    echo "The string is empty";
} else {
    echo "The string is not empty";
}

Direct comparison

$str = '';
if ($str === '') {
    echo "The string is empty";
} else {
    echo "The string is not empty";
}

Check if array key exists or not

array_key_exists() function

$array = array(
    "key1" => "value1",
    "key2" => "value2",
    "key3" => "value3"
);

$keyToCheck = "key2";

if (array_key_exists($keyToCheck, $array)) {
    echo "The key '$keyToCheck' exists in the array.";
} else {
    echo "The key '$keyToCheck' does not exist in the array.";
}

Check if an array is empty or not

empty() function

$arr = array();
if (empty($arr)) {
    echo "The array is empty";
} else {
    echo "The array is not empty";
}

count() function

$arr = array();
if (count($arr) == 0) {
    echo "The array is empty";
} else {
    echo "The array is not empty";
}

sizeof() function

$arr = array();
if (sizeof($arr) == 0) {
    echo "The array is empty";
} else {
    echo "The array is not empty";
}

Add elements to an array

$array[] = $value

<?php
$array = array();

$array[] = "element1";
$array[] = "element2";
$array[] = "element3";

print_r($array);
?>

array_push()

<?php
$array = array();

array_push($array, "element1");
array_push($array, "element2");
array_push($array, "element3");

print_r($array);
?>

Connect to mysql database

MySQLi

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database_name";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";
?>

PDO

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database_name";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>