Learning PHP and MySQL

Chapter 11 Code

Friday, August 18, 2006

Example 11-01 Displaying a number in binary format

<?php
printf
("The  computer  stores  the  number  42  internally  as  %b.",42);
?>

This code then produces the output shown in Figure 11-1.

Posted by krautgrrl on 08/18 at 12:21 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-02 printf puts the numbers into the string

<?php
printf
("The  computer  stores  the  numbers  42,  and  256  internally  as  %b  and  %b.",
42,256);
?>

When called from a web browser, the code in Example 11-2 displays Figure 11-2.

Posted by krautgrrl on 08/18 at 12:27 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-03 Displaying the same number in different formats

<?php
$value
=42printf("%d<br>",$value); 
printf("%b<br>",$value); 
printf("%c<br>",$value); 
printf("%f<br>",$value); 
printf("%o<br>",$value); 
printf("%s<br>",$value); 
printf("%x<br>",$value); 
printf("%X<br>",$value);
?>

The last column of Table 11-1 was generated with the code in Example 11-3.

Example 11-3 gives us this column:

42
101010
*
42.000000
52
42
2a
2A

Posted by krautgrrl on 08/18 at 12:28 PM
Chapter 11 Code • (3) Comments • (0) TrackbacksPermalink

Example 11-04 Using left zero padding

<?php
printf
("Zero  padding  can  help  alignment  %05d.",  42);
?>

Padding with zeros gives us the result shown in Figure 11-3.

Posted by krautgrrl on 08/18 at 12:30 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-05 Using left space padding

<?php
printf
("Space  padding  can  be  tricky  in  HTML  %  5d.",  42);
?>

Using the left space padding displays the screen shown in Figure 11-4.

Posted by krautgrrl on 08/18 at 12:31 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-06 Adding the and tags so the spaces display

<?php
printf
("<pre>Space  padding  can  be  tricky  in  HTML  %  5d.</pre>",  42);
?>

In Figure 11-5, we correctly see the spaces.

Posted by krautgrrl on 08/18 at 12:33 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-07 Left padding using the default of spaces

<?php
printf
("<pre>Space  padding  can  be  tricky  in  HTML  %5d.</pre>",  42);
?>

This code is equivalent to Example 11-5, and produces the same result, shown in Figure 11-6.

Posted by krautgrrl on 08/18 at 12:34 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-08 Right padding with spaces

<?php
printf
("<pre>Space  padding  can  be  tricky  in  HTML  %-5d.</pre>",  42);
?>

The output from the negative number in the padding field displays Figure 11-7.

Posted by krautgrrl on 08/18 at 04:02 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-09 Displaying a real number in money format

<?php
printf
("Please  pay  $%.2f.  ",  42.4242);
?>

Our code displays with the dollar sign and decimal correctly, as shown in Figure 11-8.

Posted by krautgrrl on 08/18 at 04:03 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-11 Calculating the length of a string

<?php
     $password
="secret1";

     if  (
strlen($password)  <=  5)
     
{
        
echo("Passwords  must  be  a  least  5  characters  long.");
      
}
      
else  {
         
echo  ("Password  accepted.");
       
}
?>

This password code above displays the screen in Figure 11-11.

Posted by krautgrrl on 08/18 at 05:52 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-12 Using the word case functions

<?php
   $username
="John  Doe";
    echo(
"$username  in  upper  case  is  ".strtoupper($username).".<br>");
    echo(
"$username  in  lower  case  is  ".strtolower($username).".<br>");
    echo(
"$username  in  first  letter  upper  case  is  ".ucwords($username).".<br>");
?>

The code in Example 11-12 displays lowercase, uppercase, and other details.

Posted by krautgrrl on 08/18 at 05:54 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-13 Detecting whether a string is contained in another string

<?php
   $password
="secretpassword1";

    if  (
strstr($password,"password")){
     
echo('Passwords  cannot  contain  the  word  "password".');
  
}
else  {
   
echo  ("Password  accepted.");
  
}
?>

Example 11-13 outputs the following:

Passwords cannot contain the word “password”.

Posted by krautgrrl on 08/18 at 05:56 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-14 Using several functions together to extract a portion of a string

<?php
   $test_string
="testing  testing  Username:Michele  Davis";
   
$position=strpos($test_string,"Username:");

    
//add  on  the  length  of  the  Username:
   
$start=$position+strlen("Username:");

   echo  
"$test_string<br>";
   echo  
"$position<br>";
   echo  
substr($test_string,$start);
?>

Posted by krautgrrl on 08/18 at 05:58 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-15 A simple echo of the timestamp

<?php
$timestamp
=  time();
echo  
$timestamp;
?>

Posted by krautgrrl on 08/18 at 05:59 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Example 11-16 Making the date and time appear like we expect

<?php
$timestamp
=  time();
echo  
date("m/d/y  G.i:s",$timestamp);
?>

This code returns Figure 11-14.

Posted by krautgrrl on 08/18 at 06:00 PM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink
Page 1 of 2 pages  1 2 >

Statistics

This page has been viewed 374242 times
Page rendered in 0.2073 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 13
Total anonymous users: 0
Most Recent Visitor on: 02/06/2012 06:43 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm

Referrers

Powered by ExpressionEngine