// First way and most commonly preferred <?php // PHP code goes here ?> // Second way also known as short tags <? // PHP code goes here ?> // Third way <script language="php"> // PHP code goes here </script>
echo
<?php $decision = ($age>18) ? 'can vote' : 'can not vote' ; ?>By using if statement we can write the above code as :
<?php if($age>18) { $decision = 'can vote'; } else { $decision = 'can not vote'; } ?>
<IfModule mod_php5> php_value max_execution_time 360 php_value memory_limit 128M </IfModule >
<head> <meta http-equiv="refresh" content="15"> </head>
<head> <meta http-equiv="refresh" content="15" URL="target.php"> </head>
<?php $colors = array("a"=>"red", "b"=>"green", "c"=>"blue", "d"=>"yellow"); var_dump($colors); /* Output: array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" } */ ?>
<?php $colors = array("a"=>"red", "b"=>"green", "c"=>"blue", "d"=>"yellow"); print_r($colors); /* Output: Array ( [a] => red [b] => green [c] => blue [d] => yellow ) */ ?>
<?php define("MINSIZE", 50); echo MINSIZE; // we can also use constant function to retrieve value of constant. echo constant("MINSIZE"); ?>
<?php $capitals = array("India"=>"New Delhi", "China"=>"Beijing", "Srilanka"=>"Colombo"); ?>
<?php $name = "katrina"; $katrina = "Salman"; echo $name //Output:- katrina echo $$name //Output:- Salman ?>
<?php function students() { $no_of_arguments = func_num_args(); echo "No Of Arguments = $no_of_arguments"; } student($name, $age, $address); // function call /* Output: No Of Arguments = 3 */ ?>
<?php $arrayName = array("course1"=>"php", "course2"=>"html"); $new_array = array_flip($arrayName); print_r($new_array); /* Output : Array ( [php] => course1, [html] => course2 ) */ ?>
<?php $data = '<p>Web Development</p>'; echo $profession = strip_tags($data); // It will remove the <p> tag from output. ?>
<?php echo nl2br("Thanks for visiting codephponline.com /n See you soon ."); /* Output : Thanks for visiting codephponline.com
See you soon . */ ?>
/usr/bin/php index.php
<?php $varArray = array("course1" => "PHP", "course2" => "JAVA", "course3" => "HTML"); extract($varArray); echo "$course1, $course2, $course3"; ?>
$filename = $_FILES['image']['name']; $ext = pathinfo($filename, PATHINFO_EXTENSION);
$filename = $_FILES['image']['name']; $array = explode('.', $filename); $ext = end($array);
<?php call_user_func(function() { echo 'anonymous function called.'; }); // Output: anonymous function called function barber($type) { echo "You wanted a $type haircut, no problem\n"; } call_user_func('barber', "mushroom"); // You wanted a mushroom haircut, no problem call_user_func('barber', "shave"); // You wanted a shave haircut, no problem barber('mushroom'); // You wanted a mushroom haircut, no problem barber('shave'); // You wanted a shave haircut, no problem ?>
<?php $arr = array('name'=>'angel', 'age'=>'23', 'city'=>'delhi', 'profession'=>'php developer'); $first_value = current($arr); /* Output : angel */ ?>
<?php $email = admin@codephponline.com $result = strstr($email , '@'); // Output: @codephponline.com ?>
<?php $mystring = "lets knowit"; $find = "knowit"; $result = strpos($mystring, $find); // OUTPUT: 6 ?>
<?php $arr = array('name'=>'angel' ,'city'=>'delhi', 'profession'=>'web developer'); $lastValue = end($arr); // Output : web developer ?>
<?php $startTime= microtime(true); /** Write here you code to check **/ /** Write here you code to check **/ $endTime= microtime(true); echo 'Time Taken to execute the code:'.$endTime-$startTime ?>
<?php $array = array('one','two'); echo json_encode($array); //use json_decode for decode ?>
<?php function cmp($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } $a = array(3, 2, 5, 6, 1); usort($a, "cmp"); foreach ($a as $key => $value) { echo "$key: $value\n"; } /* Output : 0: 1 1: 2 2: 3 3: 5 4: 6 */ ?>
<?php $obj1 = new Customer(); $obj2 = new Customer(); $obj3 = new Customer(); ?>
<?php Class foo { public static $variable_name = 'it is a static variable'; } echo foo :: $variable_name; // Output : it is a static varaible ?>
<?php spl_autoload_register(function ($classname) { include $classname . '.php'; }); $object = new Class1(); $object2 = new Class2(); ?>
<?php $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://www.google.com/search?q=curl"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HEADER, 0); $output = curl_exec($curl); curl_close($curl); echo $output; ?>
<?php $postData = array( "firstname" => "rahul", "lastname" => "yadav", "emailid" => "admin@codephponline.com" ); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://www.example.com"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); $output = curl_exec($curl); curl_close($curl); echo $output; ?>
Technical Architect
Kuala Lumpur, Malaysia
I'm a Full Stack Web Developer working in a MNC and passionate of developing modern web and mobile applications.
I have designed and developed CodephpOnline & CodephpOnline Wiki platform to expatiate my coding and technology learning experiences.
In my leisure time, I write technical articles on web development such as PHP, Laravel, CodeIgniter, Mediawiki, Linux, Angular, Ionic, ReactJS, NodeJS, AJAX, jQuery, Cloud and more.