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...


Types

Boolean

Boolean false implicitly converts to an empty string. In order to get 'false' when boolean variable is false, we need to do the following conversion manually:

print($myarray_is_empty ? 'true' : 'false');


Arrays


 <?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 

 

What happens if foreach is used on undefined variable ($myarray):

foreach($myarray as $element) {
   print $element;
}



Output:

Warning: Undefined variable $myarray in Standard input code on line 8

Warning: foreach() argument must be of type array|object, null given in Standard input code on line 8


if ($myarray) { // 12
    print $myarray;
}

Output:

Warning: Undefined variable $myarray in Standard input code on line 12


if ($myarray == null) { // 16

    echo '$myarray == null';
}

 

Output:

Warning: Undefined variable $myarray in Standard input code on line 16
 

 

 

PHP array_diff() Function



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.


RegEx

PHP: preg_match - Manual

PHP preg_match(): Regular Expressions (Regex)

 

$input = '--parameter-1=3 --parameter-2=bing.com --parameter-3=0';
preg_match('~parameter-1=(.*?) ~', $input, $output);
echo var_dump($output[1]);

preg_match('~parameter-2=(.*?) ~', $input, $output);
echo var_dump($output[1]);

preg_match('~parameter-3=(.*?)$~', $input, $output);
echo var_dump($output[1]);

Output:
 
string(1) "3"
string(8) "bing.com"
string(1) "0"

---

No comments: