printf("This is a number: %d", 535 );
printf("First number: %d<br>
Second number: %d<br>
", 25, 36 );
printf("Decimal: %d<br>", $number);
printf("Binary: %b<br>", $number);
printf("Double: %f<br>", $number);
printf("Octal: %o<br>", $number);
printf("String: %s<br>", $number);
printf("Hex (lower): %x<br>", $number);
printf("Hex (upper): %X<br>", $number);
Books
CDs
Games
Magazines
Left aligned
Books
CDs
Games
Magazines
Code in PHP:
<pre>
printf("%20s
", "Books");
printf("%20s
", "CDs");
printf("%20s
", "Games");
printf("%20s
", "Magazines");
printf("%- 20s
", "Left aligned");
printf("%-20s
", "Books");
printf("%-20s
", "CDs");
printf("%-20s
", "Games");
printf("%-20s
", "Magazines");
</pre>
Using printf() to format a list product pricesName Price
Green armchair 222.40
Candlestick 4.00
Coffee table 80.60
Code in PHP:
$products = Array( "Green armchair"=>"222.4","Candlestick"=>"4","Coffee table"=>80.6);
print "<pre>";
printf("%- 20s%23s
", "Name", "Price");
printf("%'- 43s
", "");
foreach ( $products as $key=>$val )
printf( "%- 20s%20.2f
", $key, $val );
printf("</pre>");
$dosh = sprintf("%.2f", 2.334454);
print "<br>You have $dosh dollars to spend";
print"<br>";
print "<br>String as an array<br>";
$test = "scallywag";
print "$test = $test<br>";
print "$test[0] = $test[0]<br>";
print "$test[2] = $test[2]<br>"
;$len = strlen($test);
print "strlen($test) = $len<br>";
$membership = "pAB7";
print "$membership = $membership<br>";print "Findng AB: strstr( $membership, "AB" )<br>";
if( strstr( $membership, "AB" ) )
print "Thank you. Don't forget that your membership expires soon!";
else
print "Thank you!";
print"<br><br>Finding the Position of a Substring with strpos()<br>";
$membership = "mz00xyz";
print "$membershop = $membership<br>";
print "Finding position of "mz": strpos($membership, "mz")";
if ( strpos($membership, "mz") === 0 )
print "<br>hello mz";
$test = "scallywag";
print "$test = $test<br>";
$s = substr($test,6);
print "substr($test,6) = $s<br>";
$s = substr($test,6,2);
print "substr($test,6,2) = $s<br>";
$test = "ququqa@quyubbte.co.uk";
print "$test = $test<br>";
print "Extracting last 3 characters: substr( $test, - 3 )<br>";
if ( $test = substr( $test, - 3 ) == ".uk" )
print "Don't forget our special offers for British customers";
else
print "Welcome to our shop!";
$test = "http://www.degreggja.com/qs.xp?OP=dnquery.xp&ST=MS&DBS=2&QRY=developer+php";
$delims = "?&";
$word = strtok( $test, $delims );
while ( is_string( $word ) )
{
if ( $word ) print "$word<br>";
$word = strtok( $delims);
}
text = lots of room to breath
lots of room to breath
print "<pre>";
$text = " lots of room to breath ";
print "text = $text<br>";
$text = trim( $text );
print $text;
print "</pre>";
Replacing Substrings Using str_replace
$string = Site contents copyright 1999. The 1999 Guide to All Things Good in the world
Site contents copyright 2000. The 2000 Guide to All Things Good in the world
Converting Case
$membership = mz00xyz after strtoupper():
MZ00XYZ
$home_url = WWW.GFIOEPRTGJOIPJGLFDGJK.CO.UK after strtolower():
http://www.gfioeprtgjoipjglfdgjk.co.uk
Code in PHP:
MZ00XYZ = "mz99xyz";
print "$membership = $membership<br>";
$membership = substr_replace( $membership, "00", 2, 2 );
print "New membership number: $membership&lp;p>";
print "Replacing Substrings Using str_replace<br>";
$string = "Site contents copyright 1999. ";
$string .= "The 1999 Guide to All Things Good in the world";
print "$string = $string<br>";
print str_replace("1999","2000",$string);
print "<br><br>Converting Case<br>";
$membership = "mz00xyz";
print "$membership = $membership after strtoupper():<br>";
$membership = strtoupper( $membership );
print "$membership<P>";
$home_url = "WWW.GFIOEPRTGJOIPJGLFDGJK.CO.UK";
print "$home_url = $home_url after strtolower():<br>";
$home_url = strtolower( $home_url );
if ( ! ( strpos ( $home_url, "http://" ) === 0 ) )
$home_url = "http://http://www.gfioeprtgjoipjglfdgjk.co.uk";
print $home_url;