miércoles, 18 de febrero de 2026

named functions an default arguments and null handling

<?php


  function Test(?string $name=null, ?int $age=null){

    

    $name= $name??"Guest";

    

    $age=$age??0;

    

    

    echo "Hello my name is $name my age is $age \n";

  }

    Test(age:44, name:"Ambiorix");

    


  ?>

Output:

9 ms | 18.4 MB
Hello my name is Ambiorix my age is 44 

martes, 17 de febrero de 2026

Oop Guide practice

<?php






class Human{

  



   public static int $globaID=1000;  

  

   private  int $intanceNumId;

   

   private  string $name;

  

  

  

  public function __construct(string $name){


$this->intanceNumId=self::$globaID+=1;

$this->name=$name;

    

  }

  

  function getId(){

    

    return $this->intanceNumId;

  }

  

  function getName(){

    return $this->name;

  }

}


$Test0 =  new Human("Class 0");


$Test1 =  new Human("Class 1");


echo  " Global  class ID ".Human::$globaID." , instance name {$Test0->getName()}  \n";


echo  "--- Instance ID  {$Test0->getId()}\n";


echo  "--- Instance ID  {$Test1->getId()}\n";


?>


Output:


 Global  class ID 1002 , instance name Class 0  
--- Instance ID  1001
--- Instance ID  1002

sábado, 20 de diciembre de 2025

try catch finally

<?php


function rankUser($age) {

    $rank = null;

    $status = null;


    try {

        if ($age >= 18 && $age <= 65) {

            $status = "allowed";


            if ($age < 45) {

                $rank = "Rookie";

            } else {

                $rank = "Veteran";

            }


        } else {

            $status = "denied";

            throw new Exception("Age must be between 18 and 65\n");

        }


    } catch (Exception $e) {

        echo 'Caught exception: ' . $e->getMessage() . "\n";

    } finally {

        if ($rank) {

            echo "User status $status, and Rank $rank";

        } else {

            echo "Sorry, your status is $status. You don't meet age requirements.";

        }

    }

}


rankUser(13);


?>


viernes, 12 de diciembre de 2025

Global variables insdide and outside a function

 <?php

$somevar = 15;

function addit() {

 GLOBAL $somevar;

 $somevar++;

 print "Somevar is $somevar";

}

addit();


?>



<?php

$somevar = 15;

function addit() {

 $GLOBALS["somevar"]++;

}

addit();

print "Somevar is ".$somevar;


?>

Static variables

 <?php

function keep_track() {

 STATIC $count = 0;

 $count++;

 print $count;

 print "<br>";

}

keep_track();

keep_track();

keep_track();

?>

Error reporting

<?php

// -------------------------------

// PHP Error Reporting Example

// -------------------------------


// Report all errors except user warnings and notices

error_reporting(E_ALL & ~E_USER_WARNING & ~E_USER_NOTICE);


// Display errors on screen (for development)

ini_set('display_errors', 1);


// Add a prefix to each error message

ini_set('error_prepend_string', '<strong>Error:</strong> ');


// Example errors

echo $undefined_variable;                 // Notice (ignored)

trigger_error("This is a warning", E_USER_WARNING);  // User warning (ignored)

trigger_error("This is an error", E_USER_ERROR);     // User error (shown with prefix)

?>

jueves, 4 de diciembre de 2025

 $options = array(

    CURLOPT_URL => 'http://www.example.com/',

    CURLOPT_RETURNTRANSFER => true,  // boolean

    CURLOPT_HEADER => false,         // boolean

    CURLOPT_USERAGENT => ''          // empty string, will be filtered

);


// Filter only non-empty strings

$options = array_filter($options, fn($v) => gettype($v) === 'string' && strlen($v) > 0);


$ch = curl_init();

foreach($options as $opt => $val){

    curl_setopt($ch, $opt, $val);

}


$response = curl_exec($ch);

curl_close($ch);