Learning PHP and MySQL
Thursday, August 03, 2006
Example 05-01 The ubiquitous Hello world!
<?php
echo ("Hello world!");
?>
Example 05-02 Displaying information about the PHP environment
<?
php phpinfo();
?>
Example 05-03 Creating an md5 signature
<?php
$mystring = "mystring";
$signature = md5($mystring);
echo $signature;
?>
Example 05-04 Using the string capitalization functions
<?php
// Capitalize a string
function capitalize( $str )
{
// First, convert all characters to lower case
$str = strtolower($str);
// Second, convert the first character to upper case
$str{0} = strtoupper($str{0});
echo $str;
}
capitalize("hEllo WoRld!" );
?>
Example 05-05 Creating a capitalize function with a default parameter $each
<?php
// Capitalize a string or the first letter of each word
function capitalize( $str, $each=TRUE )
{
// First, convert all characters to lower case
$str = strtolower($str);
if ($each === TRUE) {
$str = ucwords ($str);
} else {
$str = strtoupper($str);
}
echo ("$str <br>");
}
capitalize("hEllo WoRld!");
echo ("Now do the same with the each parameter set to FALSE.<br>");
capitalize("hEllo WoRld!",FALSE);
?>
Example 05-06 Modifying capitalize to take a reference parameter
<?php
function capitalize( &$str, $each=true )
{
// First, convert all characters to lower case
$str = strtolower($str);
if ($each === true) {
$str = ucwords($str);
} else {
$str{0} = strtoupper($str{0});
}
}
$str = "hEllo WoRld!";
capitalize( &$str );
echo $str;
?>
Example 05-07 A sample include file called add.php
<?php
function add( $x, $y )
{
return $x + $y;
}
?>
Posted by krautgrrl on 08/03 at 08:20 AM
Chapter 5 Code • (0) Comments • (247462) Trackbacks • Permalink
Chapter 5 Code • (0) Comments • (247462) Trackbacks • Permalink
Example 05-08 Using the include function
<?php
include('add.php');
echo add(2, 2);
?>
Example 5-9. Using include_once to include a file
<?php
include_once('add.php');
include_once('add.php');
echo add(2, 2);
?>
Friday, August 18, 2006
Example 05-09 Using include_once to include a file
<?php
include_once('add.php');
include_once('add.php');
echo add(2, 2);
?>
Thursday, August 03, 2006
Example 05-10 Creating an object from the Cat class
<?php
class Cat {
}
$fluffy = new Cat();
echo "Fluffy is a new ".gettype($fluffy)."!";
?>
Posted by krautgrrl on 08/03 at 08:21 AM
Chapter 5 Code • (0) Comments • (30434) Trackbacks • Permalink
Chapter 5 Code • (0) Comments • (30434) Trackbacks • Permalink
Example 05-11 Creating the Cat constructor
<?php
class Cat {
// Constructor
function Cat() {
}
}
?>
Example 05-12 Using the PHP 5 style constructor
<?php
class Cat {
// Constructor
__construct(){
}
}
?>
Posted by krautgrrl on 08/03 at 08:21 AM
Chapter 5 Code • (1) Comments • (100) Trackbacks • Permalink
Chapter 5 Code • (1) Comments • (100) Trackbacks • Permalink
Example 05-13 Defining three member functions for Cat
<?php
Class Cat {
// Constructor
function Cat() {
}
// The cat meows
function meow() {
echo "Meow...";
}
// The cat eats
function eat() {
echo "*eats*";
}
// The cat purrs
function purr() {
echo "*Purr...*";
}
}
?>
Example 05-14 Creating a new object without saving the reference
This is: Creating a new object without saving the reference, then creating an object to keep around.
<?php
new Cat;
//the Cat object cannot be accessed since its reference wasn’t saved
$myCat=new Cat;
//this time we've kept the new Cat object available
?>
Example 05-15 Adding the $age variable to Cat
<?php
class Cat {
// How old the cat is
var $age;
}
?>
Statistics
This page has been viewed 187006 times
Page rendered in 0.4646 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: 10
Total anonymous users: 0
Most Recent Visitor on: 01/06/2009 07:50 pm
The most visitors ever was 1103 on 11/20/2007 12:50 pm
