SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Strings (I)

PHP Example #1

PHP says hello and prints text strings

Well, welcome to the first of my PHP examples... In these examples, source code is displayed in the green boxes, while the result of executing the code is displayed in the yellow boxes. In this example, you can read the explanations in the very yellow box.

<?php
print "Hello, World! ";
print 'This is a demonstration of how PHP prints text strings. Print doesn\'t add any line break and the strings will be printed one following each other. You can use echo instead of print for the same purpose. ';
print 'We\'ll need to scape the single quotes when our text string contains them. ';
print ' Use \\ to scape in a string. ';
print 'And yes, we can use single or double quotes for marking strings. But double quotes admit more special characters than single quotes. ';
print nl2br("So it is time to test a line break: \n We use \\n to indicate a
line break and the nl2br() function to force the " . "browser to not ignore the whitespace created. ");
print 'We can join strings ' . 'by using the character .';
?>
Hello, World! This is a demonstration of how PHP prints text strings. Print doesn't add any line break and the strings will be printed one following each other. You can use echo instead of print for the same purpose. We'll need to scape the single quotes when our text string contains them. Use \ to scape in a string. And yes, we can use single or double quotes for marking strings. But double quotes admit more special characters than single quotes. So it is time to test a line break:
We use \n to indicate a line break and the nl2br() function to force the browser to not ignore the whitespace created. We can join strings by using the character .

PHP Example #2

PHP says hello and user name

Writing a static code is not a very exciting use of PHP. In this example, we will see how PHP can recognize text data sent from a form and attach it to a text string.

Here the form makes a call to an external script written into a .php file, whose source code can be seen below. The output will be displayed in a blank page, so hit Back to return to this webpage after seeing the result.

Your name:
<?php
print "Hello, ";
// Prints what is sent in the parameter of the form named as 'user'
print $_POST['user'];
print "!";
?>

PHP Example #3

Printing a list

Printing HTML code is one of the most powerful features of PHP and it is precisely this what can turn a static webpage into a dynamic one.

<?php
print '<ul>
<li>Beef Chow-fun</li>
<li>Sauteed Pea Shoots</li>
<li>Soy Sauce Noodles</li>
</ul>';
?>
  • Beef Chow-fun
  • Sauteed Pea Shoots
  • Soy Sauce Noodles

PHP Example #4

Validating a string

The example source code shows the usage of the trim() and strlen() functions for validating text strings. The next example will show the practical approach of this.

<?php
// $_POST['zipcode'] contains the value sent by the form named as "zipcode"
$zipcode = trim($_POST['zipcode']);
// Now $zipcode contains the value with both initial and final spaces trimmed
$zip_lenght = strlen($zipcode);
// Complains when the ZIP code doesn't has 5 characters long
if ($zip_lenght != 5) {
print "Please enter a ZIP code that is 5 characters long.";
}
?>

PHP Example #5

Validating a string

Write a ZIP code that is 5 characters long to get a confirmation of valid text or otherwise to get an error message.

Here the form makes a call to an external script written into a .php file, whose source code can be seen below. The output will be displayed in a blank page, so hit Back to return to this webpage after seeing the result.

Your ZIP code:
<?php
if (strlen(trim($_POST['zipcode'])) != 5) {
print "Please enter a ZIP code that is 5 characters long.";
}
else {
print "This ZIP code is valid, it has 5 characters long.";
}
?>

PHP Example #6

Comparing strings

Write president@whitehouse.gov to get a confirmation message.

Here the form makes a call to an external script written into a .php file, whose source code can be seen below. The output will be displayed in a blank page, so hit Back to return to this webpage after seeing the result.

Your E-mail:
<?php
if ($_POST['email'] == 'president@whitehouse.gov') {
print "Welcome, Mr. President!";
}
?>

PHP Example #7

Comparing strings

Write president@whitehouse.gov to get a confirmation message. In this example you can mix uppercase and lowercase characters without altering the result.

Here the form makes a call to an external script written into a .php file, whose source code can be seen below. The output will be displayed in a blank page, so hit Back to return to this webpage after seeing the result.

Your E-mail:
<?php
if (strcasecmp($_POST['email'], 'president@whitehouse.gov') == 0) {
print "Welcome, Mr. President!";
}
?>