Home

Introduction to Strings

Fundamentals of Strings

A Series of Characters

A string is a combination of characters or symbols. Before using a string in your code, declare a variable. Here is an example:

<?php $title ?>

Initializing a String Variable

If you declare a string variable but do not initialize it, its default value is empty. There are different ways you can specify the value of the variable. As seen in the first lesson, a string can be included in double-quotes. Here is an example:

<?php $title = "Database Administration" ?>

As an alternative, you can put the string in single-quotes.

Many web controls use strings. This includes the input text box that we saw already in Lesson 2. HTML provides many others.

Formatting a String

Formatting a string consists of inserting placeholders that represent external values. The primary way to do this is to insert string variables inside a string. Here is an example:

<!DOCTYPE html>

<html>
<head>
<title>Bethesda Car Rental</title>

<head>
<body>

<h2>Bethesda Car Rental</h2>

<?php
    $customerName = "Philippe Fields";

    echo "Dear $customerName, thank you for trusting us with your car rental needs.";
?>

</body>
</html>

This would produce:

Formatting a String

In this case, when the webpage is presented to the visitor, the variable placeholder would be replaced by the value of the variable. In the same way, you can create as many placeholders as you want in a string or somewhere in your PHP code, and the variable/placeholders can be made for different types of values. Here is an example that uses number-based variables:

<!DOCTYPE html>
<html>
<head>
<title>Geometry: Rectangle</title>
<style type="text/css">

#formulation
{
    margin: auto;
    width:  200pt;
}
#main-title
{
    font-size:   18pt;
    font-weight: bold;
    font-family: Georgia, "Times New Roman", Times, serif;
}
</style>
</head>
<body>

<?php

     $width  = 44.28;
     $height = 28.16;
     $perimeter = ($width + $height) * 2.00;
     $area      = $width * $height;

echo "<form name='frmCalculation' method='post'>
      <div id='formulation'>
      <p id='main-title'>Geometry: Rectangle</p>
     <table border='4'>
        <tr>
          <td>Width:</td>
          <td>
	    <input name='txtWidth' type='text' 
	           style='width: 55px' value=' $width ' /></td>
        </tr>
        <tr>
          <td>Height:</td>
          <td>
	    <input name='txtHeight' type='text' 
	           style='width: 55px' value=' $height ' /></td>
        </tr>
        <tr>
          <td>Perimeter:</td>
          <td><input name='txtPerimeter' type='text' value=' $perimeter ' /></td>
        </tr>
        <tr>
          <td>Area:</td>
          <td><input name='txtArea' type='text' value=' $area ' /></td>
        </tr>
      </table>
    </div>
  </form>";
?>

</body>
</html>

This would produce:

Formatting a String

You can also use values from web controls.

Escape Sequences

An escape sequence is a technique to put some characters inside a string so the parser would interpret them specially. The string must be included in double-quotes. Escape sequences use a backslash followed by one or more characters. This combination represents a particular character. The escape sequences are:

Escape Result
\' Single Quote
\" Double Quote
\n Linefeed
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\e Escape
\f Form Feed
\\ One Backslash
\$ Dollar Symbol
\0... Octal Sequence
\x... Hexadecimal Sequence
\u... Unicode Sequence

Introductory Operations on Strings

Concatenating Two Strings

Concatenating two strings consists of adding one to another. This is done using the period operator. Here is an example:

<!DOCTYPE html>

<html>
<head>
<title>Fun Department Store</title>

<head>
<body>

<h2>Fun Department Store</h2>

<?php
    $number = "50";
    $symbol = "%";
    $discount = $number.$symbol;

echo "Our store is currently selling everything at a $discount discount. Hurry Up!!!"
?>
</body>
</html>

This would produce:

Concatenating Two Strings

Although not required, you can, and should, put an empty space on both sides of the period, as in $discount = $number . $symbol.

Concatenating a String and Another Value

You can also use the period operator to concatenate a string to another type of value, or a string to a variable that holds a value other than a string. Here are examples:

<?php
    $side = 28.46;
    $perimeter = $side * 4.00;
    $area = $side * $side;

    echo "Side: " . $side;
    echo "<br>";
    echo "Perimeter: " . $perimeter;
    echo "<br>";
    echo "Area: " . $area 
?>

This would produce:

Concatenating Two Strings

Augmenting a String

As seen with numeric values, to add a new string to an existing string, you can assign the string to itself, followed by a period and the desired value. Here is an example:

<?php
$name = 'Naomy';

echo "Employee First Name: ";
echo $name;
echo "<br>";

$name = $name . " Kande";

echo "Employee Name: ";
echo $name
?>

This would produce:

Augmenting a String

As an alternative, PHP provides an operator as .= that performs the same operation. To use it, add it after the variable and followed by the string or value to add. Here is an example:

<?php
$name = 'Naomy';

echo "Employee First Name: ";
echo $name;
echo "<br>";

$name .= " Kande";

echo "Employee Name: ";
echo $name
?>

The Heredoc and the Nowdoc Techniques

Creating a String Using the Heredoc Syntax

PHP provides another technique to declare and initialize a string variable. It is referred to as heredoc and it uses the following formula:

$variable-name = <<<label-name
. . .;
label-name

Start by declaring a variable. The name of the variable is followed by = <<<. Then create a section delimited by a label of your choice. This is just a name, created like the name of a variable but without $. The name of the label is used twice, in the beginning and at the end. The first instance of the label must be attached to <<< (no space). The line with = <<<label must be on its own line. The second instance of the label must be on its own line and it must not be indented (it must be written in the beginning of its line). Inside the delimiters, define your string on its own line, which can be a paragraph of any length. The string is not quoted. Here is an example:

<!DOCTYPE html>

<html>
<head>
<title>Chemistry: Helium</title>

<head>
<body>

<?php
    $introduction = <<<Message
        Helium is a chemical element found, or used, in solids, in liquids, and in 
        gases. As a solid substance, helium is found in natural gases under the 
        earth from where it can be transformed as a form or energy. In another solid 
        form, helium is used in welding sticks where it is burned to produce heat 
        and is released as a gas. As a gas, helium is used in balloons (party or 
        commercial) to make them float in the air. Helium is the second most common 
        substance on earth (behind hydrogen). It has no color, no odor, and no taste.;
Message
?>

Once this has been done, you can use the variable where you want. Here is an example:

<!DOCTYPE html>

<html>
<head>
<title>Chemistry: Helium</title>

<head>
<body>

<?php
    $introduction = <<<Message
        Helium is a chemical element found, or used, in solids, in liquids, and in 
        gases. As a solid substance, helium is found in natural gases under the 
        earth from where it can be transformed as a form or energy. In another solid 
        form, helium is used in welding sticks where it is burned to produce heat 
        and is released as a gas. As a gas, helium is used in balloons (party or 
        commercial) to make them float in the air. Helium is the second most common 
        substance on earth (behind hydrogen). It has no color, no odor, and no taste.
Message
?>

<h2>Chemistry: Helium</h2>

<?php
    echo $introduction
?>

</body>
</html>

This would produce:

Creating a String Using the Heredoc Technique

The heredoc technique behaves the same as if the string were included in double-quotes. In fact, the string can include formats such as escape sequences or placeholders for variables, and the parser would interpret them appropriately. Here is an example that includes a placeholder for a variable:

<!DOCTYPE html>

<html>
<head>
<title>Bethesda Car Rental</title>

<head>
<body>

<h2>Bethesda Car Rental</h2>

<?php
    $customerName = "Philippe Fields";

    $message = <<<Delimiter
        Dear $customerName, thank you for trusting us with your car rental needs.
Delimiter
?>

<?= $message ?>
</body>
</html>

This would produce:

Formatting a String

Creating a String Using the Nowdoc Syntax

As an alternative to the heredoc technique that is similar to a double-quote string, PHP provides the nowdoc syntax. It uses the following characteristics:

Strings-Based Web Controls

A Label

In a web form filled with controls, to indicate what a control is used for, you can add text close to it, usually to the top or the left of the control. So far, we used simple text to do it. A label is static text that displays information to the reader. To support labels, HTML provides a control of the same name. Based on this, to get a label, create an element that uses a tag named label. The start tag must be close. Here is an example:

<html>
<head>
<title>Exercise</title>

<head>
<body>

<form name="frmExercise">

<label></label>

</form>

</body>
</html>

In the same way, you can create as many labels as you want on a form.

Besides the text it displays, a label has another important characteristics: it gives access to a web control using the keyboard. To do this, it has a feature referred to as access key. An access key is a letter, a symbol, or a digit whose key the user can press, along with Alt, to give focus to the control. To support access keys, the label element has an attribute named accesskey. Its value must be a letter, a digit, or a symbol. Here is an example:

<html>
<head>
<title>Quadrilaterals: The Square</title>

<head>
<body>

<form name="frmSquare">

<h4>Quadrilaterals: The Square</h4>

<label accesskey="s">Side</label>

</form>

</body>
</html>

To use the access key, the user would press Alt and the access key. The actual role of a label is to give access to a control. This means that you must indicate the control to which the label leads. To support this, the label element has an attribute named for. The value of that attribute must be the identification of the control. Here are examples:

<!DOCTYPE html>

<html>
<head>
<title>Quadrilaterals: The Rectangle</title>

<style type="text/css">
.btn { width: 60pt }
</style>

</head>
<body>

<form name="frmRectangle">

<h4>Quadrilaterals: The Rectangle</h4>

<table style="width:200px;">
  <tr>
    <td><label accesskey="w" for="width">Width:</label></td>
    <td><input type="text" id="width" name="txtWidth" size="10" value="0.00"></td>
    <td></td>
  </tr>
  <tr>
    <td><label accesskey="h" for="height">Height:</label></td>
    <td><input type="text" id="height" name="txtHeight" size="10" value="0.00"></td>
    <td><input type="button" name="btnCalculate" value="Calculate" class="btn"></td>
  </tr>
  <tr>
    <td>Area:</td>
    <td><input type="text" id="area" name="txtArea" size="10" value="0.00"></td>
    <td><input type="reset" class="btn"></td>
  </tr>
</table>

</form>

</body>
</html>

The last suggestion is to underline the letter that serves as the access key. This iseasily done by including the letter in a u element. This can be done as follows:

<!DOCTYPE html>

<html>
<head>
<title>Quadrilaterals: The Rectangle</title>

<style type="text/css">
.btn { width: 60pt }
</style>

</head>
<body>

<form name="frmRectangle">

<h4>Quadrilaterals: The Rectangle</h4>

<table style="width:200px;">
  <tr>
    <td><label accesskey="w" for="width">Width:</label></td>
    <td><input type="text" id="width" name="txtWidth" size="10" value="0.00"></td>
    <td></td>
  </tr>
  <tr>
    <td><label accesskey="h" for="height">Height:</label></td>
    <td><input type="text" id="height" name="txtHeight" size="10" value="0.00"></td>
    <td><input type="button" name="btnCalculate" value="Calculate" class="btn"></td>
  </tr>
  <tr>
    <td>Area:</td>
    <td><input type="text" id="area" name="txtArea" size="10" value="0.00"></td>
    <td><input type="reset" class="btn"></td>
  </tr>
</table>

</form>

</body>
</html>

This would produce:

Access Keys

In this case, to access the Width text box, the visitor can press Alt + W. To access the Height text box, the visitor can press Alt + H.

A Text Area

A text area is a large control that is a substitute to handle a long paragraph of text. To get a text area, create an element using the <TextArea> tag which must be closed. Here is an example:

<!DOCTYPE html>

<html>
<head>
<title>Chemistry: Helium</title>

<head>
<body>

<?php
$introduction = <<<Message
Helium is a chemical element found, or used, in solids, in liquids, and in gases. 
As a solid substance, helium is found in natural gases under the earth from where 
it can be transformed as a form or energy. In another solid form, helium is used 
in welding sticks where it is burned to produce heat and is released as a gas. As 
a gas, helium is used in balloons (party or commercial) to make them float in the 
air. Helium is the second most common substance on earth (behind hydrogen). It has 
no color, no odor, and no taste.
Message
?>

<h2>Chemistry: Helium</h2>

<?php
    echo "<textarea> $introduction </textarea>"
?>
</body>
</html>

This would produce:

A Text Area

The dimensions of a text area are the main properties that set it apart from the text box control. The width of a text area is set using the COLS attribute of the <textarea> tag. Therefore, to increase the width of the text area, assign the desired numeric value to the cols attribute. The width is measured by the number of characters that can be displayed on one line of text. By default, a width of a text area is set to 20 characters. To change this, you can assign a higher value to the attribute. Here is an example:

<!DOCTYPE html>

<html>
<head>
<title>Chemistry: Helium</title>

<head>
<body>

<?php
$introduction = <<<Message
Helium is a chemical element found, or used, in solids, in liquids, and in gases. 
As a solid substance, helium is found in natural gases under the earth from where 
it can be transformed as a form or energy. In another solid form, helium is used 
in welding sticks where it is burned to produce heat and is released as a gas. As 
a gas, helium is used in balloons (party or commercial) to make them float in the 
air. Helium is the second most common substance on earth (behind hydrogen). It has 
no color, no odor, and no taste.
Message
?>

<h2>Chemistry: Helium</h2>

<?php
    echo "<textarea cols='55'> $introduction </textarea>"
?>
</body>
</html>

This would produce:

A Text Area

The height of a text area is measured by the number of visible lines of text the text area can display. By default, the text area is configured to display two lines of text. This property is controlled by the ROWS attribute. To change or increase this number, assign an appropriate numeric value to the rows attribute. You can specify either one of the COLS or the ROWS values. You can also specify both to control the dimensions of the text area. When using both attributes, there is no order of the attributes. Here is an example:

<!DOCTYPE html>

<html>
<head>
<title>Chemistry: Helium</title>

<head>
<body>

<?php
$introduction = <<<Message
Helium is a chemical element found, or used, in solids, in liquids, and in gases. 
As a solid substance, helium is found in natural gases under the earth from where 
it can be transformed as a form or energy. In another solid form, helium is used 
in welding sticks where it is burned to produce heat and is released as a gas. As 
a gas, helium is used in balloons (party or commercial) to make them float in the 
air. Helium is the second most common substance on earth (behind hydrogen). It has 
no color, no odor, and no taste.
Message
?>

<h2>Chemistry: Helium</h2>

<?php
    echo "<textarea cols='55' rows='8'> $introduction </textarea>"
?>
</body>
</html>

This would produce:

A Text Area

If you are planning to access the text area from your code, you should give the control a name.

The Password Text Box

HTML provides a special text box designed for a form's password. This looks like a classic text box and is created the same way. The main difference is that the text that the user types doesn't display the characters that are being entered. As the user types in the password text box, text appears as a series of asterisks or periods. Here is an example:

This would produce:

The Password Text Box

To create a password text box, use the <input> tag and specify the control type as password assigned to the type attribute. Here is an example:

<input  type="password">

The width of the password text box is controlled by the size attribute of the <input> tag. To increase the width of the control, assign the desired numeric value to the size attribute. Here is an example that creates a text box control:

<input type="password" size="25">

The text that is typed in the password text box is held by the value attribute. You can use this attribute to find out what password the user typed.

If you plan to retrieve the password value in a script or code of any kind, give the control a name. This is done by assigning a name string to the name attribute. Here is an example:

<input type="password" name="Guarded">

Previous Copyright © 2015-2016, FunctionX Next