Categories

What version of PHP

I was asked how to get the version of PHP , this was due to the minimum requirements of a script stating PHP 5 and  a user wanted to know how he could check what his host supplied.

This is the simplest version, save as version.php, upload and  then open it in your web browser.

<?php
echo phpversion();
?>

On [...]

PHP version

This will display the version of PHP you are using

<?php
echo “<br>I am using “.PHP_VERSION;
?>

Explode A File Example

The test text file was actually an article, this example will look for full stops in the text and output each line as a new line.

<?php
   $file = file_get_contents(“test.txt”);

   $contents = explode(“.”,$file);
   foreach ($contents as $content)
   {
   echo “$content<br/>”;
   }
?>

Read a text file into an array

Read a text file into an array

<?php
   $file = file_get_contents(“test.txt”);

   // Place the contents of test.txt in an array
   $contents = explode(“\n”,$file);

   foreach ($contents as $content)
   {
      echo “$content”;
   }
?>

Add to an array

Add to an array using array_push

<?php
   $search = array(“google”,”yahoo”,”live”,”google”,”yahoo”,”google”);
   print_r($search);
   print “<br>”;
   //lets add a couple of values and print them out again
   array_push($search,”google”,”live”);
   print_r($search);
?>

The result will be

Array ( [0] => google [1] => yahoo [2] => live [3] => google [4] => yahoo [5] => google )
Array ( [0] => google [1] => yahoo [...]

Count array values

This example counts the values in an array using the array_count_values function

<?php
   $search = array(“google”,”yahoo”,”live”,”google”,”yahoo”,”google”);
   $frequency = array_count_values($search);
   print_r($frequency);
?>