PHP

Basic Programs in PHP

The following article shows Basic Programs in PHP.

Examples on Expressions, Decision Making, and Loops

Read and Display a String

<?php
  $a = readline('Enter your name: ');
  // For output
  echo 'Welcome' . $a; 
?>

Decision Making Example

<?php
  $a = 34;
  if($a%2==0)
    echo 'even number';
  else   
    echo 'odd number';
?>

Output

even number

Using Nested-if Statement

<?php
  $x=67;
  $y=88;
  $z=43;
  if(($x<$y) && ($x<$z))
  {
      echo $x.' is smallest!';
  }
  else if(($y<$x) && ($y<$z))
  {
       echo $y.' is smallest!';
  }
  else
  {
       echo $z.' is smallest!';
  }

?>

Output

43 is smallest!

While Loop Example – Displaying Table of a Number

<?php
$n=6;
echo '<br/>Table of '.$n;
$i=1;
?>
<table>
<?php    
while($i<=10)
{
    $f=$n*$i;
    echo '<tr>';
    echo '<td>'.$n.'</td><td>X</td><td>'.$i.'</td><td>'.$f.'</td>';
    echo '</tr>';
    $i=$i+1;
}
?>
</table>

Output

Table of a Number in PHP
Table of a Number in PHP

Using switch..case Statement in PHP

<?php
    $p=4;
    $a=8896;
    $b=45;
    switch($p)
    {
        case 1:
            echo 'Sum = '.($a+$b);
            break;
        case 2:
            echo 'Difference = '.($a-$b);
            break;
        case 3:
            echo 'Multiplication = '.($a*$b);
            break;
        default:
            echo 'Division = '.($a/$b);
            break;
    }
?>

Output

Division = 197.68888888889

Creating Arrays in PHP

<?php
echo 'Arrays in PHP<br/>';
$arr1=array();
for($i=0;$i<10;$i=$i+1)
{
    $arr[$i]=($i+1)*($i+2);
    echo $arr[$i].'<br/>';
}
?>

Output

Arrays in PHP
2
6
12
20
30
42
56
72
90
110

Example of array_slice() Function

<?php
echo 'Array Slice<br/>';
$arr2=array(1,2,3,4,5,6,7,8);
print_r(array_slice($arr2,4));
?>

Output

Array Slice
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 )

Example of array_splice() Function

Basically, we use the array_splice() function to remove a specific part of the array and to replace it with other values. It takes four parameters. While the first parameter is the given array on which we apply this function. On the other hand, the second parameter represents an offset that indicates the start index for removing elements. The third parameter represents how many array elements will be removed. Whereas, the last parameter represents the replacement array. Furthermore, this function doesn’t preserve the replacement keys. Finally, it returns extracted elements in an array.

Example 1

<?php
  echo 'Original Array...<br/>';
  $a1=array(1,2,3,4,5,6,7,8,9,10);
  foreach($a1 as $value)
  {
      echo "$value<br/>";
  }
  print_r(array_splice($a1,0, 3));
?>

Output

Original Array…
1
2
3
4
5
6
7
8
9
10
Array ( [0] => 1 [1] => 2 [2] => 3 )

Example 2 – Positive and Negative Offsets

The following example shows that the positive offset of 1 extracts the elements at index 1, 2, and 3 respectively. While the negative offset of -10 extracts elements at index 0, 1, 2 since the negative offset represents offset from the end. Therefore offset of -1 represents the removed portion from the last element and extracts the last element as shown below.

<?php
  echo 'Setting offset to 1...<br/>';
  $a1=array(1,2,3,4,5,6,7,8,9,10);
  print_r(array_splice($a1,1, 3));
  
  echo '<br/>Setting offset to -10...<br/>';
  $a1=array(1,2,3,4,5,6,7,8,9,10);
  print_r(array_splice($a1,-10, 3));

  echo '<br/>Setting offset to -1...<br/>';
  $a1=array(1,2,3,4,5,6,7,8,9,10);
  print_r(array_splice($a1,-1, 3));
?>

Output

Setting offset to 1…
Array ( [0] => 2 [1] => 3 [2] => 4 )
Setting offset to -10…
Array ( [0] => 1 [1] => 2 [2] => 3 )

Setting offset to -1…
Array ( [0] => 10 )

Example 3 – Setting the length to 0, -ve, +ve, and omitted

The following example shows four different cases for the value of length. While 0 value for length doesn’t remove any element. On the other hand, omitting length removes all elements. Similarly, a positive value of length removes that number of elements from start. However, when the length is negative, it indicates the end of the removed portion. Therefore, when the length is -3, only the last 3 elements are left in the array.

<?php
  echo 'Setting length to 0...<br/>';
  $a1=array(1,2,3,4,5,6,7,8,9,10);
  print_r(array_splice($a1,0, 0));
  
  echo '<br/>omitting length...<br/>';
  $a1=array(1,2,3,4,5,6,7,8,9,10);
  print_r(array_splice($a1,0));
  
  echo '<br/>Setting length to 3...<br/>';
  $a1=array(1,2,3,4,5,6,7,8,9,10);
  print_r(array_splice($a1,0, 3));
  
  echo '<br/>Setting length to -3...<br/>';
  $a1=array(1,2,3,4,5,6,7,8,9,10);
  print_r(array_splice($a1,0, -3));
  echo '<br/>Remaining elements in array...<br/>';
  print_r($a1);
?>

Output

Setting length to 0…
Array ( )
omitting length…
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )
Setting length to 3…
Array ( [0] => 1 [1] => 2 [2] => 3 )
Setting length to -3…
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 )
Remaining elements in array…
Array ( [0] => 8 [1] => 9 [2] => 10 ) 

Example 4 – Replacement Array

The following example shows how to use replacement arrays. Basically, the array_aplice() function inserts the replacement array from where it removes the elements. Therefore, in the first example, the replacement array replaces the first three elements. Hence, the remaining 7 elements appear after the replacement array. Whereas, in the second example, the length is set to 0. So, it doesn’t remove any element. Also, the offset in this example is 0. Therefore, the array_splice() function inserts the replacement array at offset 0.

<br/>
<?php
  $a2=array(45, 90, 78, 81, 32, 65);

  $a1=array(1,2,3,4,5,6,7,8,9,10);
  echo '<br/>Original Array...';
  print_r($a1);
    echo '<br/>Replacement Array...';
  print_r($a2);
  echo '<br/>Setting length to 3...<br/>';
  print_r(array_splice($a1,0, 3, $a2));
  
  echo '<br/>Resulting elements in array...<br/>';
  print_r($a1);
  
  echo '<br/>Setting length to 0...<br/>';
  $a1=array(1,2,3,4,5,6,7,8,9,10);
 
  print_r(array_splice($a1,0, 0, $a2));
  echo '<br/>Resulting elements in array...<br/>';
  print_r($a1);
?>

Output

Original Array…Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )
Replacement Array…Array ( [0] => 45 [1] => 90 [2] => 78 [3] => 81 [4] => 32 [5] => 65 )
Setting length to 3…
Array ( [0] => 1 [1] => 2 [2] => 3 )
Resulting elements in array…
Array ( [0] => 45 [1] => 90 [2] => 78 [3] => 81 [4] => 32 [5] => 65 [6] => 4 [7] => 5 [8] => 6 [9] => 7 [10] => 8 [11] => 9 [12] => 10 )
Setting length to 0…
Array ( )
Resulting elements in array…
Array ( [0] => 45 [1] => 90 [2] => 78 [3] => 81 [4] => 32 [5] => 65 [6] => 1 [7] => 2 [8] => 3 [9] => 4 [10] => 5 [11] => 6 [12] => 7 [13] => 8 [14] => 9 [15] => 10 ) 

Another example of array_splice() function is shown below. As can be seen, it specifies all four parameters.

<?php
echo '<br/>array_splice() function to remove certain elements from an array and replace them with other elements...<br/>';
$ax1=array("i"=>"A","j"=>"B","k"=>"C","l"=>"D");
$ax2=array("i"=>"abc","l"=>"pqr");
array_splice($ax1,1,3,$ax2);
print_r($ax1);
?>

Output

array_splice() function to remove certain elements from an array and replace them with other elements…
Array ( [i] => A [0] => abc [1] => pqr )


Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

PHP Projects for Undergraduate Students

Architectural Constraints of REST API

REST API Concepts

Creating a Classified Ads Application in PHP

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *