Useful Functions

By Vikram Aikat




echo( )

Basic Syntax

As echo( ) is not actually a function and is instead a language construct, with a predefined function, you do not need to use parenthesis when using it. See below:

<?php
// This will display the message in the quotes.
   echo 'Hello World.';
?>

The text after the '//' is a comment and does not affect the code. This code will output the message "Hello World." to the screen. An example of the output can be seen here:

Basic Display

You can also echo a number or string stored in a variable in the following way:

<?php
// These two lines declare the value of the variables
$name = "Shodor";
$number = 1024;
// These lines will display the variable after the echo. 
// Do not use quotes for this. If you do it will display "$name" instead of displaying the value.
   echo $name;
   echo $number;
?>

This code should output "Shodor1024". An example of this code can be seen here:

Display Variables and Strings

Adding HTML

You can insert html code into the syntax to change the look of the output. This is actually fairly simple to do, as seen below.

<?php
// This is the unchanged text
   echo 'Hello World.';
// These lines create line breaks between the words
   echo '<br />';
   echo '<br />';
// This will bolden the text
   echo '<strong>Hello World.</strong>';
   echo '<br />';
   echo '<br />';
// This will italicize the text
   echo '<i>Hello World.</i>';
   echo '<br />';
   echo '<br />';
// This will underline the text
   echo '<u>Hello World.</u>';
   echo '<br />';
   echo '<br />';
// This will underline, bolden and italicize the text
   echo '<strong><i><u>Hello World.</u></i></strong>';
?>

The code should output a the text with the different changes to it. An example of the code can be found here:

Adding HTML

Concatenation

If you want to add strings or variables together using echo, there are two ways to do it. I will be showing you both. The first uses single quotes for the echo and double quotes for the strings.

<?php
// First, declare any variables that you want to use
$wish = 1000000000;
$money = 1;
// Use the following syntax to concatenate. Each separate part is separated by a "."
   echo 'I wish I had '.$wish.' dollars, but sadly I only have '.$money.' dollar(s)';
// Use single quotes around each string and remember to keep to "$" before each variable
?>

An example of concatenation using single quotes can be found here:

Concatenation using single quotes

This is fairly easy to do, but could take time if you have a lot of strings and variables to type. Using double quotes can make the whole process easier.

<?php
// First, declare any variables that you want to use
$wish = 1000000000;
$money = 1;
// Use the following syntax to concatenate. Only use the quotes to start and end echo.
   echo "I wish I had $wish dollars, but sadly I only have $money dollar(s)";
// Use single quotes around each string and remember to keep to "$" before each variable
?>

You can just insert the variable name into the string. This makes the whole process much easier to do, as you do not need to constantly add periods. An example of concatenation using single quotes can be found here:

Concatenation using double quotes

print( )

print( ) is very similar to echo. They both are used to display information, both are not functions but language constructs and echo is a newer version of print. One important difference between the two is that print is slower than echo.

Basic Display

The following is how to display a message using print.

<?php
// The text in the double quotes is displayed
   print "I am at Shodor right now";
// This leaves a line break between the strings
   print "<br />";
// Declare a variable
   $number = 21;
// This variable will be displayed
   print $number
?>

The output can be seen here:

Basic Display using print

Concatenation with print

This is the same as with echo, and even has the same syntax, just switch echo for print!


strtolower( )

This function can be used in conjunction with echo or print to change a string. This function is used to turn all of the letters of a string to lowercase letters. See below for the syntax.

<?php
// Everything inside of the parenthesis will be converted to lowercase, provided that there are quotes are there
   echo strtolower("HeLlO WoRLd.");
?>

This will output all of the letters in lowercase, displaying "hello world.". It is really that simple! The actual output can be see here:

strtolower( )

strtoupper( )

This function can be used in conjunction with echo or print to change a string. This function is used to turn all of the letters of a string to uppercase letters. See below for the syntax.

<?php
// Everything inside of the parenthesis will be converted to uppercase, provided that there are quotes are there
   echo strtoupper("hello world.");
?>

This will output all of the letters in uppercase, displaying "HELLO WORLD.". It is really that simple! The actual output can be see here:

strtoupper( )

strpos( )

This function is used to find the position of one string inside of another. It is set up using the following syntax:

strpos(string,find,start)

In the string spot, you write any string you want, of any length. In the find spot, put the string that you want to find in the original. This function will find how how many character you have to go into the string to find the "find word". The start spot can define where you want to start your search. See below:

<?php
// This line will find out how many characters there are in the string before you reach "time"
   echo strpos("I am at Shodor and am having a fun time","am");
// This lines makes a break between the outputs
   echo "<br />";
// This line does not start the search until it reacher 12 characters, so it will ignore the first "am".
// So it counts until it reaches the second "am", which is 19 characters in
echo strpos( "I am at Shodor and am having a fun time" , "am" , 5);
?>

The output of the code will look like this:

strpos( )

exit( )

This function is used to hide all code after the exit function, and instead display a message.

<?php
// This text before the exit( ) will be run
echo "I am text that is happy to be visible";
echo "<br />";
// All text past the exit( ) will not be run
exit("Everything past this point is gone!");
echo "I just want to be seen"
// But it won't
?>	

Every line of code after the exit will not show. This is the same as using the die( ) function, just switch exit for die! An example of this can be seen here:

exit( )

substr( )

This function will take out part of a string. It can be used to take out the first however many characters in a string you want. It is very easy to use, as seen below:

<?php
   echo substr("Useless text, Hello World.",14);
?>	

This will delete the first 14 characters of the string, leaving just "Hello World.". An example of this can be seen below:

substr( )

include( )

This statement is used to call the variables and parameters of another PHP code into the current one. Let us say that you have defined 10 variables in variables.php, and you don't want to have to retype the variables on every new php file. You can use the include statement to get all of the variable values from the other file. An example of this can be seen below.

This can be very useful to increase efficiency, as you can have just one file with all of your variables, so that you do't need to redeclare them every time. This statement runs the included function, so make sure that it does not interfere and display something that will mess you up. The variable code can be seen here. It does not display anything, it only stores values to variables:

<?php
// These are the values that I am storing in variables
$number = 100;
$name = "Shodor";
?>		

The code for the include statement is below:

<?php
// This will import all of the variables form another .php file
   include 'variables.php';
// You can use these variables even though you haven't declared them in this particular file.
   echo " I work at $name. I always give $number% effort at Shodor."
?>		

The output of the include example can be seen here:

include( )

isset( )

This function is used to make sure that the form that was previously called upon was properly submitted. This can be used to make sure that the input was properly typed into the box. After it checks if the form was working, it then treats it as an if statement. See below for the syntax. This one requires a knowledge of HTML forms to use properly:

<html>
 <form action="isset.php" method="POST"><input type="textarea" name="age"><input type="submit" value="submit"></form>
 <body>
  <?php
   if(isset($_POST['age']) & $_POST['age']!=null) {
    echo "You are " . $_POST['age']. " year(s) old.";
}
?>		
 </body>
</html>		

Once it makes sure that you entered into the form correctly, it will act as though it is an if statement and echo the string. The output of the code can be seen here:

isset( )

var_dump( )

The var_dump function dumps all information about a variable. It then displays that it has taken out the old info and put in placeholders. See below for an example:

<?php
   $dumped = array( 52, 126, 546, 112);
   var_dump($dumped);
?>	

The var_dump function stored 0 into the first slot of the array, 1 into the second and so forth. This can be useful is used in a proper way. The output of the code can be found here:

var_dump( )

It can also be used to dump arrays that are inside of other arrays, as seen below

<?php
   $dumped = array( 52, 126, 546, array("hello","Shodor", 512, array(1243, "Hello World.", "bye", 1123)));
   var_dump($dumped);
?>	

When you have nested arrays or variables, the function will indent the whole process! The output of the second one can be found here:

var_dump( )

array_push( )

This function is used to push any values to the end of an array. See below for an example:

<?php
// This line defines the array
   $numbers = array(0,2,4,6,8);
// This line pushes the new values to the back of the array
   array_push($numbers,10,12,14,16);
// This formats the output
   echo "<pre>";
// This prints out the new array
   print_r($numbers);
// This formats the output
   echo "</pre>";
?>

The new values will be placed at the back of the array. The output can be seen here:

array_push( )

array_count_values( )

This function is used to count how many of each string or value an array had. See below for an example:

<?php
// This line defines the array
   $numbers = array(0,0,0,0,0,2,2,2,3,3,3,5,5,5,5,5,5,5,5,5);
// This line is used for formatting
   echo "<pre>";
// This line counts the amount of times each value appears
   print_r (array_count_values($numbers));
   echo "</pre>";
?>

The new values will be placed at the back of the array. The output can be seen here:

array_count_values( )

To find the particular value of a certain item in an array use the following syntax:

<?php
// This line defines the array
   $numbers = array(0,0,0,0,0,2,2,2,3,3,3,5,5,5,5,5,5,5,5,5);
// This line is used for formatting
   echo "<pre>";
// This line counts the amount of times each value appears
   $count = array_count_values($numbers);
   echo "</pre>";
// This will display the number of zeroes
   echo $count[0];
?>

ucwords( )

This function is used to make the first letter of every word in a string uppercase. See below for an example:

<?php
   echo ucwords("my name is shodor. i am a very shodorific person");
?>

The new display should be "My Name Is Shodor. I Am A Very Shodorific Person" The output can be seen here:

ucwords( )

simplexml_load_file( )

This function is used to convert and XML file to a SimpleXML object, and then outputs the elements of that object. See below for an example of just printing out the object's elements:

<?php
   $xmlcall=simplexml_load_file("shodor.xml");
   // This helps with formatting
   echo "<pre>";
   This prints out the elements by their respective tags
   print_r ($xmlcall);
   // This helps with formatting
   echo "</pre>";
?>

This is based on the idea that the shodor.xml is as follows:

 <?xml version="1.0" encoding="ISO-8859-1"?>
  <shodor>
   <to>Ernie</to>
   <from>Vikram</from>
   <heading>Block Schedule</heading>
   <body>When does the next block begin?</body>
  </shodor>

The output of the whole program can be seen here:

simplexml_load_file( )

strlen( )

This function is used to find the length of an string in characters. See below for an example.

<?php
   $string= "This is a really cool string that is very long and awesome";
echo strlen($string);
?>

This should display a random number between 1 and 100. The output can be seen here:

strlen( )

Math Functions




rand( )

This function is used to call a random number between the two parameters. See below for an example:

<?php
   echo rand(1,100);
?>

This should display a random number between 1 and 100. The output can be seen here:

rand( )

ceil( )

This function is used to round any number up. See below for an example:

<?php
// This will display 1
   echo ceil(.5);
// This will space out the outputs
   echo "<br />";
// This will display 5
   echo ceil(4.1111);
   echo "<br />";
// This will display 10
   echo ceil(9.99999999);
?>

This should display the rounded value of each number, rounded up. The output can be seen here:

ceil( )

floor( )

This function is used to round any number down. See below for an example:

<?php
// This will display 0
   echo floor(.5);
// This will space out the outputs
   echo "<br />";
// This will display 4
   echo floor(4.1111);
   echo "<br />";
// This will display 9
   echo floor(9.99999999);
?>

This should display the rounded value of each number, rounded down. The output can be seen here:

floor( )

round()

This function is used to round any number. Anything above .5 will round up and below will round down. See below for an example:

<?php
// This will display 1
   echo round(.5);
// This will space out the outputs
   echo "<br />";
// This will display 4
   echo round(4.1111);
   echo "<br />";
// This will display 10
   echo round(9.999);
?>

This should display the rounded value of each number. The output can be seen here:

round( )

exp( )

This function is used raise the number 'e' (approx. 2.718282) to any power that is in the argument. See below for an example:

<?php
// This will display 'e'
   echo exp(1);
// This will space out the outputs
   echo "<br />";
// This will display 'e'^4
   echo exp(4);
   echo "<br />";
// This will display 'e'^10
   echo exp(9);
?>

This should display the value of 'e' to the xth power, with x being the argument. The output can be seen here:

exp( )

fmod( )

This function is used to find the remainder after one number is divided by another number. See below for an example:

<?php
   $x = 9;
   $y = 2;
   $r = fmod($x, $y);
// This will display 1, as the remainder when 9 is divided by 2 is 1
   echo $r;
   echo "<br />";
   $a = 20;
   $b = 4;
   $c = fmod($a, $b);
// This will display 0, as the remainder when 20 is divided by 4 is 0
   echo $c;
?>

The output can be seen here:

fmod( )

hypot( )

This function is used to find the hypotenuse of a triangle given the side lengths of the legs of the triangle. See below for an example:

<?php
   $x = 3;
   $y = 4;
   $r = hypot($x, $y);
// This will display 5, as the square root of 3*3 + 4*4 is 5
   echo $r;
   echo "<br />";
   $a = 12;
   $b = 5;
   $r = hypot($a, $b);
// This will display 13, as the square root of 12*12 + 5*5 is 13
   echo $r;
?>

The output can be seen here:

hypot( )

sqrt( )

This function is used to find the square root of a number. See below for an example:

<?php
   $x = 9;
   $s = sqrt($x);
// This will display 3, as the square root of 9 is 3
   echo $r;
   echo "<br />";
   $y = 121;
   $z = sqrt($x);
// This will display 11, as the square root of 121 is 11
   echo $z;
?>

The output can be seen here:

sqrt( )