<?php $marks = [50,50,40,87,96,35,66,55,95,35,62,87]; $names = ["jignesh" => 15, "sandip" => 85, "narendra" => 52]; $nameMarks = [ ['name' => 'jignesh','marks' => 100], ['name' => 'Haresh','marks' => 500], ['name' => 'sunil','marks' => 800], ]; $person = ["Harsh", "Rahul", "Sunny", "Raju"]; $age = [42, 35, 78, 99]; $personAnother = ["Harish", "Rahul", "Sunny", "Rajesh", "jignesh"]; $personAnotherOne = ["Suresh", "Sahil"]; // count the number of occurance in array print_r(array_count_values($marks)); // convert key into upper\lower case print_r(array_change_key_case($names, CASE_UPPER)); // convert main array into multiple array with given number of size print_r(array_chunk($marks, 2)); //Returns perticular key's value from multidimensional array print_r(array_column($nameMarks,'name')); // Combine two array into one array as result, first array used as key, second as value print_r(array_combine($person, $age)); // Returns an array "from first array", which are not in other arrays print_r(array_diff($person, $personAnother, $personAnotherOne)); // Creates array with perticulat value param1 => starting point of array param2 => length of array, param3 => value of each key print_r(array_fill(1, 3, "php")); // Creates an array with fix value, param1 is array and param2 is value to be assignesd on each key of param1 $keys = [5,2,'framework']; print_r(array_fill_keys($keys, 'laravel')); // Exchanges all keys with their associated values print_r(array_flip($names)); // Returns the common value from two arrays print_r(array_intersect($age, $marks)); // Add at the start OR at the end of array print_r(array_pad($personAnotherOne,-4,'Uday')); print_r(array_pad($personAnotherOne,4,'Uday')); //Remove first element of array and returns the deleted element print_r(array_shift($personAnotherOne)); echo "\n"; //Add element at starting point of array array_unshift($personAnotherOne,"PHP"); print_r($personAnotherOne); echo "\n"; print_r($personAnotherOne,"PHP"); echo "\n"; //Remove last element of array print_r(array_pop($person)); echo "\n"; //Add element at the end of array array_push($person,"Sandip"); echo "\n"; print_r($person); //SOrt an array without using inbulilt function $a = [15,5,2,6,4,8,10]; echo "\n"; echo "Before :"; print_r($a); for ($i=0; $i < count($a); $i++) { for ($j=0; $j < count($a); $j++) { if($a[$j+1] > $a[$j]) { $t = $a[$j+1]; $a[$j+1] = $a[$j]; $a[$j] = $t; } } } echo "\n"; echo "After : " ; print_r($a); // Interface definition interface Animal { public function makeSound(); } // Class definitions class Cat implements Animal { public function makeSound() { echo " Meow "; } } class Dog implements Animal { public function makeSound() { echo " Bark "; } } class Mouse implements Animal { public function makeSound() { echo " Squeak "; } } // Create a list of animals $cat = new Cat(); $dog = new Dog(); $mouse = new Mouse(); $animals = array($cat, $dog, $mouse); // Tell the animals to make a sound echo "Animal interface implemented in Cat,Dog,Mouse Class with the method makeSound() \n>"; foreach($animals as $animal) { $animal->makeSound(); } echo "\n"; echo "Sort array india nic"; $arr = array(2,5,1,7,4); for($i = 0; $i < count($arr); $i++ ) { for($j = 0; $j < count($arr)-1; $j++) { echo "\n"; if($arr[$j+1] > $arr[$j]) { $temp = $arr[$j+1]; $arr[$j+1] = $arr[$j]; $arr[$j] = $temp; } } } print_r($arr); echo "array question convoso"; echo "\n"; function returnArray($numberAry, $position) { $tempAry = $tempAry1 = []; for ($i = 0; $i < count($numberAry); $i++) { if($i >= $position) { $tempAry [] = $numberAry[$i]; } else { $tempAry1 [] = $numberAry[$i]; } } for ($j = 0; $j < count($tempAry1); $j++) { $tempAry[] = $tempAry1[$j]; } return $tempAry; } $resultAry = returnArray([5,7,10,14,2,1],2); print_r($resultAry); ?>
Write, Run & Share PHP code online using OneCompiler's PHP online compiler for free. It's one of the robust, feature-rich online compilers for PHP language, running on the latest version 7. Getting started with the OneCompiler's PHP compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as PHP
and start coding.
OneCompiler's PHP online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample PHP program which takes name as input and prints hello message with your name.
<?php
fscanf(STDIN, "%s\n", $name);
echo "Hello ".$name.".\n";
?>
PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.
In PHP, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically. Variables are case-sensitive in PHP.
$variable_name = value;
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
//code
}
if(conditional-expression){
//code if condition is true
} else {
//code if condition is false
}
if(condition-expression1) {
//code if above condition is true
} elseif(condition-expression2){
//code if above condition is true
}
elseif(condition-expression3) {
//code if above condition is true
}
...
else {
//code if all the conditions are false
}
Switch is used to execute one set of statement from multiple conditions.
switch(conditional-expression) {
case value1:
// code if the above value is matched
break; // optional
case value2:
// code if the above value is matched
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
// you can use any of the below syntax
foreach ($array as $element-value) {
//code
}
foreach ($array as $key => $element-value) {
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
function function_name(parameters) {
//code
}
function_name (parameters)