Tuesday, 20 November 2018

PHP - Functions

Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it. Following example creates a function called writeMessage() and then calls it just after creating it.
Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below

<html>
   
   <head>
      <title>Writing PHP Function</title>
   </head>
   
   <body>
      
      <?php
         /* Defining a PHP Function */
         function writeMessage() {
            echo "You are really a nice person, Have a nice time!";
         }
         
         /* Calling a PHP Function */
         writeMessage();
      ?>
      
   </body>
</html>
This will display following result −You are really a nice person, Have a nice time!

Saturday, 17 November 2018

Factorial of a number in PHP

How to print factorial of any number in php.

Example of Factorial

 
5!= 5 * 4 * 3 * 2 * 1 = 120

Factorial of a number in PHP

<?php

$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--) 
{
  $factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";

?>

Output

 
Factorial of 4 is 24

Print Table Program in PHP

The simple concept to generate table of any number is


<?php  

define('NUM', 3); 

for($i=1 ; $i<=10 ; $i++) 
{ 
  echo $i*NUM; 
  echo '<br>';   
}

?>

Tuesday, 13 November 2018

Difference between array merge and array combine

array_merge merges the elements of one or more than one array such that the value of one array appended at the end of first array. If the arrays have same strings  key  then the later value overrides the previous value for that key .
<?php
$array1 = array("course1" => "java","course2" => "sql");
$array2 = array(("course1" => "php","course3" => "html");
$result = array_merge($array1, $array2);
print_r($result);
?>
OUTPUT :  
Array
(
[course1] => php
[course2] => sql
[course3] => html
)
Array_combine creates a new array by using the key of one array as keys and using the value of other array as values.
It returns the combine array of array 1 and array2 .
<?php
$array1    = array("course1","course2","course3");
$array2    = array(("php","html","css");
$new_array = array_combine($array1, $array2);
print_r($new_array);
?>
OUTPUT :
Array
(
[course1]  => php
[course2]    => html
[course3]    =>css
)

PHP - Functions

Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your ...