Thursday, 18 May 2023

My PHP notes

 


I've never worked professionally with PHP but had to test some code so here are just some snippets I had to run in tehplayground...

 <?php
// example code

// $welcome = file_get_contents('/content/welcome');

// empty array
$my_array =
array();

if ($my_array
== NULL) {
    print "\$my_array == NULL\n";
}

if ($my_array
=== NULL) {
    print "\$my_array === NULL\n";
}

if (
is_null($my_array)) {
    print "is_null(\$my_array) is true\n";
}

print "my_array = {$my_array}\n"; // line 21


print "var_dump(\$my_array) = \n";
var_dump($my_array);

if (
in_array("test", $my_array)) {
    echo "test is in array";
}


if (!in_array("test", $my_array)) {
   
echo "test is not in array";
}

print "Unsetting \$my_array";
unset($my_array);


if ($my_array == NULL) {
    print "\$my_array == NULL\n";
}

if ($my_array === NULL) { // line 44
    print "\$my_array === NULL\n";
}

if (in_array("test", $my_array)) { // line 48
    echo "test is in array";
}

?>

 

Output:

 

$my_array == NULL

Warning: Array to string conversion in Standard input code on line 21
my_array = Array
var_dump($my_array) =
array(0) {
}
test is not in arrayUnsetting $my_array
Warning: Undefined variable $my_array in Standard input code on line 40
$my_array == NULL

Warning: Undefined variable $my_array in Standard input code on line 44
$my_array === NULL

Warning: Undefined variable $my_array in Standard input code on line 48

Fatal error: Uncaught TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given in Standard input code:48
Stack trace:
#0 Standard input code(48): in_array('test', NULL)
#1 {main}
  thrown in Standard input code on line 48 

 



if (empty($not_declared_array)) {
echo("not_declared_array is empty.\n");
} else {
echo("not_declared_array is not empty.\n");
}

$null_array = null;

if (
empty($null_array)) {
echo("null_array is empty.\n");
} else {
echo('$null_array is not empty.\n');
}

$array = [];

if (
empty($array)) {
echo("array is empty.\n");
} else {
echo('array is not empty.\n');
}




Output:

not_declared_array is empty.
null_array is empty.
array is empty.

No comments: