Saturday, January 20, 2018

PHP string manipulate

Introducing String LANGUAGE



Introducing Strings Now that you’ve been introduced to the general concept of variables, let’s look at variables in detail. The first variable type to delve into is the string. A string is merely a quoted chunk of characters: letters, numbers, spaces, punctuation, and so forth. These are all strings:

•‘Tobias’
•“In watermelon sugar”
•‘100’
•‘August 2, 2011’

      To make a string variable, assign a string value to a valid variable name:

$ first_name = 'Tobias'; 

$ today = 'August 2, 2011'; 

When creating strings, you can use either single or double quotation marks to encapsulate the characters, just as you would when printing text. Likewise, you must use the same type of quotation mark for the beginning and the end of the string. If that same mark appears within the string, it must be escaped:

$ var = "Define \" platitude\", please."; 

Or you can also use the other quotation mark type:

$ var = 'Define "platitude", please.'; 

To print out the value of a string, use either echo or print:

echo $ first_name;

To print the value of string within a context, you must use double quotation marks:

echo "Hello, $ first_name"; 

You’ve already worked with strings once—when using the predefined variables in the preceding section (the values of those variables happened to be strings). In this next example, you’ll create and use your own strings.

Script 1.     String variables are created and their values are sent to the Web browser in this script.





To use strings

1. Begin a new PHP document in your text editor or IDE, to be named strings.php, starting with the initial HTML and including the opening PHP tag (Script 1.):

<! DOCTYPE html PUBLIC "-// W3C// DTD XHTML 1.0 

Transitional// EN" 
"http:// www.w3. org/ TR/ xhtml1/ DTD/ xhtml1-transitional.dtd" > 
< html 
xmlns =" http:// www.w3. org/ 1999/ 
xhtml" xml:lang =" en" lang =" en" > 
< head > 
< meta http-equiv =" Content-Type" content =" text/ html; 
charset = utf-8" /> 
< title > Strings </ title > 
</ head > 
< body >
 <? php # Script 1. - strings.php 

2.

Within the PHP tags, create three variables:

$ first_name = 'Haruki'; 
$ last_name = 'Murakami'; 
$ book = 'Kafka on the Shore'; 

This rudimentary example creates $ first_name, $ last_name, and $ book variables that will then be printed out in a message.

3. Add an echo statement:

echo "< p > The book
< em > $ book</ em > was written by 
$ first_name $ last_name. </ p >"; 

All this script does is print a statement of authorship based upon three established variables. A little HTML formatting (the emphasis on the blog’s title) is thrown in to make it more attractive. Remember to use double quotation marks here for the variable values to be printed out appropriately (more on the importance of double quotation marks at the blog end).

4.
Complete the PHP block and the HTML page:

?> </ body > </ html > 

5.
Save the file as strings.php, place it in your Web directory, and test it in your Web browser . The resulting Web page is based upon printing out the values of three variables.

6.
If desired, change the values of the three variables, save the file, and run the script again .

The output of the script is changed by altering the variables in it.

Tip

If you assign another value to an existing variable (say $ book), the new value will overwrite the old one. For example: $ book= 'High Fidelity'; $ book = 'The Corrections'; /* $ book now has a value of 'The Corrections'. */

Tip

 PHP has no set limits on how big a string can be. It’s theoretically possible that you’ll be limited by the resources of the server, but it’s doubtful that you’ll ever encounter such a problem.

Script 1. Concatenation gives you the ability to append more characters onto a string.

1.   <! DOCTYPE html PUBLIC "-// W3C// DTD XHTML 1.0 Transitional// EN" "http:// www.w3. org/ TR/ xhtml1/ DTD/ xhtml1-transitional.dtd" > 

2.     < html xmlns =" http:// www.w3. org/ 1999/ xhtml" xml:lang =" en" lang =" en" > 

3.     < head > 

4.       < meta http-equiv =" Content-Type" content =" text/ html; charset = utf-8" /> 

5.    < title > Concatenation </ title >

 6.      </ head > 

7.      < body > 

8.   <? php # Script 1. - concat.php 



10.   // Create the variables: 

11.    $ first_name = 'Melissa'; 

12.   $ last_name = 'Bank'; 

13.   $ author = $ first_name . ' ' . $ last_name; 

14.

15.    $ book= 'The Girls\' Guide to Hunting and Fishing'; 

16.

17.    // Print the values: 

18.      echo "< p > The blog< em > $ bloggers</ em > was written by $ author. </ p >"; 

19. 

20.        ?> 

21.     </ body > 

22.      </ html >

Read More 

0 comments:

Please comment and follow this site blog inbox