Bento Examples
Bento Examples
Section titled “Bento Examples”Arrays
$a1 = ["hello", "world", "!"]$a2 = array("hello", "world", "!");$a3 = explode(",", "apple,pear,peach");
Mixed integer and string values.
- a
- b
$array = array("foo" => "bar","bar" => "foo",100 => -100,-100 => 100,);print_r($array);
Functions
Common PHP functions for arrays and strings.
count($array); // Array lengtharray_push($arr, $val); // Add to endarray_pop($array); // Remove from end
String functions:
strlen($string); // String lengthsubstr($str, 0, 5); // Substringstr_replace($old, $new, $str);
Syntax
// Variables and basic syntax$variable = "Hello World";$number = 42;$array = ["item1", "item2"];
// Control structuresif ($condition) { echo "True";} else { echo "False";}
for ($i = 0; $i < 10; $i++) { echo $i;}
Classes
class Car { public $color; private $engine;
public function __construct($color) { $this->color = $color; }
public function start() { return "Engine started!"; }}
$myCar = new Car("red");echo $myCar->start();
Loops
// For loopfor ($i = 0; $i < 5; $i++) { echo $i . " ";}
// While loop$x = 0;while ($x < 5) { echo $x++;}
// Foreach loopforeach ($array as $value) { echo $value;}
// Foreach with keyforeach ($array as $key => $value) { echo "$key: $value";}
Variables
// Variable types$string = "Hello";$integer = 42;$float = 3.14;$boolean = true;$array = [1, 2, 3];
// Variable scopeglobal $globalVar;static $staticVar;