phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP Basic: String Data Type

Tag: data type, string   Category: PHP Basic
post: 01 Mar 2008 read: 218


PHP Basic Data Type Step By Step Tutorial - Part 5: String is a group of character. In PHP, a character is considered to be become a byte so that there is 256 character differ. A string literal can be expressed with three ways of differing: Single Quoted, Double Quoted, Heredoc Sintax.

Single Quoted

String can be expressed with single qoute clasp. With a single qoute, the variable which exist in it will be considered as a string literal so that value which exist in the variable do not be presented.

Pay attention to the script as following: File name : string.php


<Html>
<head>
<title>String Data Type</title>
</head>
<body>
<?
	$article = 'Using PHP';
	echo 'This is string'.'<br>';
	echo 'you also can insert newlines 
        in string like this way.'.'<br>';
	echo 'Harry say : I\'ll be back'.'<br>';
	echo 'Are you sure you want to delete D:\\*.*?'.'<br>';
	echo 'Are you sure you want to delete D:\*.*?'.'<br>';
	echo ' am trying to include at 
        this point : \rn a newline'.'<br>';
	echo '$article'.'<br>';
?>
</body>
</html>

The result of executing string.php:

php basic string data type

Double Quoted

If the string is expressed flankedly by Double Quoted, PHP recognizes the more special character (character escape). Differ from single qoute, variable which exist in Double Quoted will be presented by its value.

Table character escape

Character Info
\n Linefeed (10) in ASCII
\r Carriage return (13) in ASCII
\t Tabulation horizontall (9) in ASCII.(
\\ Backslash
\$ Dollar sign
\" Double quoted
[0-7]{,3} Character in Octal
x[0-9A-Fa-f]{1,2} Character ini Hexadecimal

Pay attention to the example of script using String data type with double quoted as following:
File name : string1.php


<Html>
<head>
<title>String Data Type</title>
</head>
<body>
<?
	$article = 'Using PHP';
	echo "This is string"."<br>";
	echo "you also can insert newlines 
        in string like this way."."<br>";
	echo "Harry say : I\"ll be back"."<br>";
	echo "Are you sure you want to delete D:\\*.*?"."<br>";
	echo "Are you sure you want to delete D:\*.*?"."<br>";
	echo '"am trying to include 
        at this point : \rn a newline"."<br>";
	echo "$article"."<br>";

?>
</body>
</html>

The result of string.php :

php basic string data type

Heredoc Sintax ("<<<")

String also can limit by using the sign of heredoc sintax ("<<<"). Identifier writes after the sign <<<, followed string, then it is covered with same identifier

Pay attention to the example of using String data type with heredoc sintax at script as following:
File name: string2.php


<html>
<head>
<title>String Data Type</title>
</head>
<body>
<h1>The using of Heredoc Sintax</h1>
<? 
$article = <<<TEST
Using PHP
TEST;
echo <<<DOC
yielded string use Heredoc Sintax<br>
\$article= $article
DOC;
?>
</body>
</html>

The result of string2.php:

php basic string data type



| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting