05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Changing Case<br />

<strong>PHP</strong> has several functions for changing the case of strings: strtolower( ) and<br />

strtoupper( ) operate on entire strings, ucfirst( ) operates only on the first character<br />

of the string, and ucwords( ) operates on the first character of each word in the<br />

string. Each function takes a string to operate on as an argument and returns a copy<br />

of that string, appropriately changed. For example:<br />

$string1 = "FRED flintstone";<br />

$string2 = "barney rubble";<br />

print(strtolower($string1));<br />

print(strtoupper($string1));<br />

print(ucfirst($string2));<br />

print(ucwords($string2));<br />

fred flintstone<br />

FRED FLINTSTONE<br />

Barney rubble<br />

Barney Rubble<br />

If you’ve got a mixed-case string that you want to convert to “title case,” where the<br />

first letter of each word is in uppercase and the rest of the letters are in lowercase, use<br />

a combination of strtolower( ) and ucwords( ):<br />

print(ucwords(strtolower($string1)));<br />

Fred Flintstone<br />

Encoding and Escaping<br />

Because <strong>PHP</strong> programs often interact with HTML pages, web addresses (URLs), and<br />

databases, there are functions to help you work with those types of data. HTML,<br />

web page addresses, and database commands are all strings, but they each require<br />

different characters to be escaped in different ways. For instance, a space in a web<br />

address must be written as %20, while a literal less-than sign (

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!