PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

CHARACTER STRINGS IN PHP

Character strings are a series of character sequences, can be combinations of numbers, punctuation characters and letters, words or sentences. The following are some examples of character strings:

$string1="gunston500";
$string2="Hello";
$string3="My name iS John Smith";
$string4='john@smith.com';

When only numbers are stored in variables, no single or double quotes are used so that these variables can be used in mathematical calculations as in the following example:

$firstNumber=156; // number
$secondNumber=44; // number
$totNumber= $firstNumber + $secondNumber; // number

When only numbers are enclosed in single or double quotes, they become character strings and cannot be used in mathematical calculations as in the following example:

$firstNumber="156"; // character string
$secondNumber='44'; // character string

You can use either single or double quotes to store Character strings in a variable but there are differences in the way PHP treats them.

Single-quoted string values are stored literally ie. as is.

Example:

$name="John";

$string='My name is $name'; // stored as: My name is $name

A \ (back slash) is used to escape an apostrophe (') in single quoted strings as the example below:

$string='My mother\'s name is $name'; // stored as: My mother's name is $name

Double-quoted string values are processed before the string is stored.

Example:

$name="John";

$string='My name is $name'; // stored as: My name is John
// ie. the $name variable is evaluated to John and then stored as: My name is John

Using Secial Characters In Double Quoted Strings

Newline \n: Characters proceeding \n start on a new line. Example:

$string="John \nSmith";
echo $string;

The above outputs:

John
Smith

Other special characters:

Carriage-return character: \r
Tab character: \t

Home | Privacy Policy | Terms Of Use | Contact Us