<?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);
?> 

PHP Online Compiler

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.

Taking inputs (stdin)

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";
?>

About PHP

PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.

Key features

  • Free
  • powerful tool for making dynamic and interactive web pages
  • can integrate with almost all popular databases like MySQL, PostgreSQL, Oracle, Sybase, Informix, Microsoft SQL Server etc.
  • C like Syntax and easy to learn.
  • Object oriented scripting language.
  • easily embeddable into HTML
  • Loosely typed language.

Syntax help

Variables

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;  

Loops

1. IF Family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression){  
//code if condition is true  
} else {  
//code if condition is false  
} 

Nested-If-else

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  
}  

2. Switch:

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;    
} 
 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

For-each:

// you can use any of the below syntax
foreach ($array as $element-value) {  
    //code  
}

foreach ($array as $key => $element-value) {   
    //code 
} 

4. While:

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 
}  

5. Do-While:

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); 

Functions

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.

How to define a Function

function function_name(parameters) {  
  //code
}

How to call a Function

function_name (parameters)