Posts

Showing posts with the label PHP

PHP arrays

* In PHP, there are three kind of arrays: * 1)Numeric array - An array with a numeric index * 2)Associative array - An array where each ID key is associated with a value * 3)Multidimensional array - An array containing one or more arrays 1)Numeric Arrays A numeric array stores each array element with a numeric index. There are two methods to create a numeric array. 1. In the following example the index are automatically assigned (the index starts at 0): $array1 = array(3,5,7,2); echo $array1[0]." "; $array2 = array(4,"dantha" ,"brown" ,array("x","y","z")); echo $array2[3][2]." "; $array2[3] = "Cat"; echo $array2[3]." "; 2. In the following example we assign the index manually: $cars[0]="Honda"; $cars[1]="Nissan"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0]." and ".$cars[1]." are Japanese cars"; 2) ASSOCIAT...

PHP Static modifiers

<?php // static modifier // with static methods you can't use $this // this : talking about current instance // self : talking about current class class Student{ static $total_students = 6; static public function add_students() { //here you can use either Student or self. //but self is better because it's inside the Student class. self::$total_students++;// self : talking about current class. } static function welcome_students($var="Hello") { echo "{$var} students"; } } //$student = new Student(); //$student-> total_students; // echo Student::$total_students." "; // echo Student::welcome_students("Good bye")." "; echo Student::add_students()." "; //Student::$total_students = 1; echo Student::$total_students." "; /* -------- VERY IMPORTANT ---------- * static variables are shared throughout the inheritance tree * even if you assign different values inside ch...