This is part 16 of my series, Basics of PHP. In this part of the series, we look at basics of PHP string and date.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
String
A string is a series of characters. A string literal can be specified in 4 different ways, but we shall look only at two: single quoted and double quoted. The argument in the echo construct below is a string literal in single quotes:
echo 'His name is John.';
The argument below is a string literal in double quotes:
echo "His name is $hisVar";
With double quotes, the value of a variable in the string literal will appear, in place of the variable, when the string is displayed. So if we had,
$hisVar = "Peter";
then the above string will be displayed as, "His name is Peter". With single quotes, the value of a variable in the string literal does not replace the variable. With single quotes, the variable name including the $ sign is displayed and not the value.
Let us now look at some string functions.
The strlen Function
The strlen function returns the length of a string as number of characters, including spaces. The syntax is:
int strlen (string $string)
The following statement displays 14.
echo strlen("I am a string.");
Of course, you can assign the return value of the function to a variable, and then use the variable for some other manipulation.
The strpos Function
Character position counting in a string begins from zero. Character position of a character in a string is also called the index position. The strpos function returns the position of the first occurrence of a sub string. In simple terms, the syntax is:
int strpos(mainString, subString)
The following statement displays, 3:
echo strpos("We are dancing.", "are");
The substr Function
Remember, character position counting in a string begins from zero. The substr function returns a sub string whose start position is given. The syntax is:
string substr ( string $string , int $start [, int $length ] )
If the optional length parameter is omitted, the sub string from the start position to the end is returned.
The following statement displays, "are".
echo substr("They are dancing.", 5, 3);
The main string is "They are dancing." The start position is 5. The length (number of characters) is 3. So "are" is displayed.
There are many string functions. I have given you just three. The string functions are predefined in PHP. You can consult some other document for the other functions.
Date
There are many predefined functions in PHP you can use to handle dates. In this article, I will only show you how to obtain the current date and time. You use the predefined date function. This function obtains the current date and time of the computer that is running the PHP program. Since PHP is normally run at the server, you can use this function to obtain the current date and time of the server computer and send to the client browser. In simple terms, the syntax is:
string date ( string $format )
The function returns a string whose content is the current date and time. The parameter, the function takes is also a string. Try the following code, first before we look at the explanation:
In the first statement, the right operand reads the current date and time and assigns the value in string form to the variable, $myDate. The second statement echoes the date and time in string form. The value displayed will be something like:
08-13-2009,22:08:47
Let us now look at the argument of the date function. The argument (parameter) is called the String Format, for the current date and time. m in the argument, stands for the current month. The hyphen in the argument is displayed. d stands for current day of the month. The hyphen after d is displayed. Y stands for the current year in four digits. The comma after Y is displayed. H stands for the current 24-Hour. The colon after H is displayed. i stands for minutes. The colon after i is displayed. s stands for seconds. The letters, m, d, Y, H, I, and s must be typed in the same cases as given. You should compare the argument of the date function with the example display given.
Let us stop here and continue in the next part of the series.
Chrys
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- Boolean Logic and PHP ConditionsIn this part of the series we apply Boolean logic to PHP conditions.
- Capitalizing a String in PHPUsing the UCWords function in PHP to capitalize strings.
- PHP Object Oriented Programming OOP for BeginnersA basic introduction to PHP object oriented programming geared towards beginners.
- PHP: Getting Text to Display from ODBC DatabaseThis will help PHP users who have text field output truncated at 4096 bytes when connecting to a MS SQL database via an ODBC connection.
How to Produce Good and Resource-friendly PHP CodeThe ease of PHP, the pop web development language of the day, makes the potential for producing disasters so easy - these tips on good and resource-friendly coding will help you...
- PHP Function Basics
- PHP Basic Syntax
- PHP Regular Expressions
- Basics of PHP Variables
- PHP Comparison and Arithmetic Operators
- PHP Array
- PHP Loop Statements



