sábado, 29 de noviembre de 2025

viernes, 21 de noviembre de 2025

Array map

 <?php

function cube($n, $r)

{

    return ($n * $n * $n) / $r;

}


$a = [1, 2, 3, 4, 5];

$c = [1, 2, 3, 4, 5];


$b = array_map('cube', $a, $c);


print_r($b);

?>

miércoles, 19 de noviembre de 2025

array filter

 $arr=[12,3,90,4];


function Myfunc($k){

return $k>80;

}

$result=array_filter($arr,"Myfunc",0);

  

  print_r($result);

sábado, 3 de mayo de 2025

last value

<?php

$arr=[13,4,91,8];


foreach( $arr as $k=>$v){

 

 if(end($arr)==$arr[$k]){

  

   echo "This is the last value $v no pending value";

   

 }

  else {

       echo  "$v  and next value is  {$arr[$k+1]} <br>";


  }

  

}

?> 

----------------

<?php

$arr = [13, 4, 91, 8];

$lastIndex = count($arr) - 1;


foreach ($arr as $k => $v) {

    if ($k === $lastIndex) {

        echo "This is the last value $v, no pending value";

    } else {

        echo "$v and next value is {$arr[$k + 1]} <br>";

    }

}

?>

13 and next value is 4
4 and next value is 91
91 and next value is 8
This is the last value 8, no pending value

sábado, 5 de abril de 2025

Class interface

 // Defining the interface

interface BicycleInterface {

    // Method declarations (no implementations)

    public function changeCadence($newValue);

    public function changeGear($newValue);

    public function speedUp($increment);

    public function applyBrakes($decrement);

    public function printStates();

}






// Implementing the BicycleInterface in a class

class Bicycle implements BicycleInterface {

    private $cadence = 0;

    private $speed = 0;

    private $gear = 1;


    // Implementing methods from BicycleInterface

    public function changeCadence($newValue) {

        $this->cadence = $newValue;

    }


    public function changeGear($newValue) {

        $this->gear = $newValue;

    }


    public function speedUp($increment) {

        $this->speed += $increment;

    }


    public function applyBrakes($decrement) {

        $this->speed -= $decrement;

    }


    public function printStates() {

        echo "Cadence: " . $this->cadence . ", Speed: " . $this->speed . ", Gear: " . $this->gear . "\n";

    }


}


// Example usage

$bicycle = new Bicycle();

$bicycle->changeCadence(50);

$bicycle->changeGear(3);

$bicycle->speedUp(20);

$bicycle->applyBrakes(5);

$bicycle->printStates();  // Outputs: Cadence: 50, Speed: 15, Gear: 3

Clsss Inheritance:

 <?php


// Parent class

class Vehicle {

    public $brand;

    public $model;

    

    // Constructor

    public function __construct($brand, $model) {

        $this->brand = $brand;

        $this->model = $model;

    }


    // Method in parent class

    public function start() {

        echo "The vehicle is starting.\n";

    }

}


// Child class inheriting from the Vehicle class

class Car extends Vehicle {

    public $doors;

    

    // Constructor in child class

    public function __construct($brand, $model, $doors) {

        // Call parent class constructor to initialize properties

        parent::__construct($brand, $model);

        $this->doors = $doors;

    }


    // Method in child class

    public function honk() {

        echo "The car is honking.\n";

    }


    // Overriding the parent method

    public function start() {

        echo "The car is starting.\n";

    }

}


// Creating an instance of the Car class

$car = new Car("Toyota", "Corolla", 4);


// Accessing inherited properties

echo "Brand: " . $car->brand . "\n";  // Outputs: Brand: Toyota

echo "Model: " . $car->model . "\n";  // Outputs: Model: Corolla

echo "Doors: " . $car->doors . "\n";  // Outputs: Doors: 4


// Calling methods

$car->start();  // Outputs: The car is starting.

$car->honk();   // Outputs: The car is honking.


?>


domingo, 23 de marzo de 2025

php class example

 class Hombre {

  private $name;

  private $age;


  // Constructor to initialize the name and age

  function __construct($name, $age = 1) {

    // If the name length is less than 3, generate a unique ID as the name

    $this->name = (strlen($name) < 4) ? $name.'_'.uniqid() : $name;

    $this->age = $age;

    echo "$this->name has been created<br>"; 

  }


  // Method to change the name

  function changeName($name) {

    $this->name = $name;

  }


  // Method to get the name

  function getName() {

    return $this->name; 

  }


  // Method to get the age if needed

  function getAge() {

    return $this->age;

  }

}


// Creating an object with the name "a" and age 34

$m = new Hombre('a', 34);  // name will be replaced with a unique ID


// Changing the name to "Mario Rodriguez"

$m->changeName('Mario Rodriguez');


// Outputting the new name

echo $m->getName();  // Outputs: Mario Rodriguez