So far we have only displayed strings directly to the browser without changing the format. We have successfully done that using the PHP echo () command. In addition to the echo () command, PHP also has two other print functions that allow users to format the output. The formatting may come in the form of rounding numbers to a certain number of decimal places, aligning strings or numeric values to the left or right and so on. The first formatting command we will use id the printf ().
Using PHP printf ()
This command is used to format and send the output to the browser for display. The general format for printf () is as follows:
printf ("My favourite number is: %d",20);
// this statement here prints:
My favourite number is: 20
printf () has two critical components, one is the part that has what is called conversion specification and the other part has the value on which the conversion specification is to be applied. The conversion specification starts with the symbol (% ). Bellow is a list of common conversion specifications that are used with printf ():
| Name | Description |
| =========== | ==================================================================== |
| %b | Displays integers as binary numbers |
| %d | Display whatever argument as a decimal number |
| %f | Displays integer as floating point (with specified number of decimal places) |
| %s | Display the argument as a string |
| %c | Display ASCII equivalent of an integer |
Example:
<?php
$variable = 123;
printf("Binary Value: %b ", $variable);
printf("Double Value: %d ", $variable);
printf("Decimal Value: %f", $variable);
printf("String Value: %s", $variable);
printf("Characcter Value: %c ", $variable);
?>
You can also use the printf () function to specify the field width. In this case we are specifying that the string field’s width should be 15. To do this we would use the following PHP statement:
$variable = "Mununga";
printf("String Value: %15s ", $variable);
By default, printf aligns the values to the right, but if you would like to align the values to the left, you put a minus ( - ) symbol to the left of the of the number, but right after the % symbol:
printf("String Value: %-15s ", $variable);
PHP string functions
PHP has several built-in functions for manipulating string values. This section will cover some of the most commonly used string functions.
Finding the length of a string - strlen ()
To find the length of a string, you use the strlen () function. The general format is:
strlen(string_value);
The most common use of such a function is in evaluating the length of a string and doing something based on the returned value. For example, We would only want to display the name of an individual when the length of their name is at least 1 characters long. To do this in PHP you would use the following group of statements:
if( strlen($customername) > 0)){
echo $customername;
}
This will print the customer name based on the value of the variable $customername, if this value is assigned a value with the length greater than 0 then the name will be displayed, otherwise the program does nothing.
Extracting part of a string with - substr ()
This function is used to extract a number of characters from a string, starting at a specified position. The general format for substr () is:
substr(string_value,starting_position, length);
In some cases you may skip the length value. When that happens, you are telling the program to extract everything from the specified string that comes after the specified position. For example:
$stringvalue = "we are here for you";
echo substr($stringvalue,6); // returns "here for you"
echo substr($stringvalue,6,4); // returns "here"
Converting case with - strtoupper () and strtolower ()
To convert a string to upper case you use the strtoupper () function. To convert to lower case you use strtolower () function. For example,
$firstname = "Simon";
echo strtolower($firstname); //prints "simon"
echo strtoupper($firstname); //prints "SIMON"
Replacing substring with - str_replace ()
This function replaces a specified string part with another in a given string value. The simplest format is:
str_replace(old_value, new_value, string_value);
This replaces every occurrence of the old value with the new value in the given string value.
You could also use this function with a set of array values, Say now that you have sets of values that you need to substitute for other values in a string. What you would then do is to put the old values in an array, and do the same with the new values. The order is very important here, the order of the values you need to replace and the replacing values has to be the same. Once you have your two arrays, you can then use the str_replace function to replace the old values in the array with the new ones. Here is an example:
$stringvalue = "We had 350 cows last year, but now we are down to 250";
$oldvalues = array("350","250","cows");
$newvalues = array("185","160","goats");
$newstring = str_replace($oldvalues, $newvalues, $stringvalue);
echo $newstring; // this prints "We had 185 goats last year, but now we are down to 160"
|