Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL

Home > PHP > Stringsprinter version

PHP Strings

Strings are so commonly used in PHP that there are a number of library functions provided to manipulate them.

String Literals

String literals may be enclosed with single or double quotes but behave differently if variables are included within the string.

With double quotes the variable can be written directly into the string - with single quotes concatenation must be used.

Examples of Single and Double Quotes
<?php
    $name = "George";
    $age = 35;

    // Using double quotes
    echo ("His name is $name aged $age years");

    // Using single quotes
    echo ('His name is '.$name.' aged '.$age." years");
?>


Escape Sequences

Escape sequences are preceded by the \ character to signify that the characters are to be treated differently.

In a single quoted string if you wish to use the ' character then to avoid PHP treating the character as a string delimiter then it must be escaped with the \ character.

The only other escape sequence supported by single quoted strings is the \ character itself, which if used, must be escaped with \.

Single Quotes Escape Sequences
<?php
    echo ('It\'s a single quote string.');

    echo ('Backslash \\ is used as an escape character.');
?>


Double quoted strings support a number of escape sequences including ...



Finding the Length of a String

The strlen() function returns the number of characters in a string.

Example of strlen()
<?php
    $name = "George";

    // This prints out 6
    echo (strlen ($name));
?>


Changing Case

You can change the case of all or part of a string with the following functions.



Comparing Strings

PHP provides a number of functions for comparing two strings to determine if they are equal or one is greater than the other.

A string which is greater than another is one whose characters appear later in the alphabet. e.g. Fred is greater than Frank.

The strcmp() Function

This takes two strings as arguments and returns ...



Example of strcmp()
<?php
    $name1 = "George";
    $name2 = "George";

    // This prints out 0
    echo (strcmp ($name1, $name2));
?>


The strncmp() Function

This works in the same way as strcmp() except it accepts an extra length argument to tell the function to restrict the comparison to the number of characters stated.

Example of strncmp()
<?php
    $name1 = "George";
    $name2 = "Georgina";

    // This prints out 0
    echo (strcmp ($name1, $name2, 6));
?>


The functions strcasecmp() and strncasecmp() do the same as strcmp() and strncmp() but are not case sensitive.

Previous - running a script Previous - Expressions & Operators     Next - Strings & Substrings Next - strings and substrings


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
PHPBasicsCreating a ScriptRunning a ScriptVariablesExpressions and OperatorsPHP stringsStringsStrings and SubstringsReplacing SubstringsRegular ExpressionsBranches and ConditionsLoopsArraysArray FunctionsSorting ArraysUser-Defined FunctionsInclude and RequireUploading FilesFile FunctionsSession Variables
Recommended Reading
PHP and MySQL web development

programming PHP

PHP cookbook
Tips and TricksPage TemplatesForm Reply ScriptsForm ValidationJavaScript to PHP
Got any PHP Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.