Learning PHP and MySQL
Wednesday, July 19, 2006
Example 03-05 Using comments to make your code easier to read
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php
// A single line comment could say that we are going to
// print hello world.
/* This is how to do a
multi-line comment and could be used to comment out a block of code */
echo("Hello world!<br />");
print('Goodbye.<br />');
?>
</body>
</html>
Chapter 3 Code • (1) Comments • (368) Trackbacks • Permalink
Example 03-07 The default handling of variable scope
<?php
// define a function function birthday(){
// Set age to 1
$age = 1;
}
// Set age to 30
$age = 30;
// Call the function
birthday();
// Display the age echo $age;
?>
Displays:
30
Chapter 3 Code • (5) Comments • (788) Trackbacks • Permalink
Example 03-08 Using a global variable changes the result
<?php
// Define a function function birthday(){
// Define age as a global variable
global $age;
// Add one to the age value
// set age to 30
$age = 30;
// Call the function birthday();
// Display the age echo $age;
?>
Displays:
31
Chapter 3 Code • (3) Comments • (97) Trackbacks • Permalink
Example 03-09 A static variable remembering its last value
<?php
// Define the function function birthday(){
// Define age as a static variable static $age = 0;
// Add one to the age value
$age = $age + 1;
// Print the static age variable
echo "Birthday number $age<br />";
}
// Set age to 30
$age = 30;
// Call the function twice birthday();
birthday();
// Display the age
echo "Age: $age<br />";
?>
Displays:
Birthday number 1
Birthday number 2
Age: 30
Chapter 3 Code • (4) Comments • (654) Trackbacks • Permalink
Example 03-10 PHP_SELF being used with a file called test.php
<?php
echo $_SERVER["PHP_SELF"];
?>
Outputs:
/test.php
Chapter 3 Code • (4) Comments • (503) Trackbacks • Permalink
Example 03-11 Working with strings
<?php
$my_string = "Margaritaville - Suntan Oil Application!";
echo "Margaritaville - Suntan Oil Application!";
?>
Chapter 3 Code • (4) Comments • (1002) Trackbacks • Permalink
Example 03-12 Using a variable in a string definition
<?php
$my_string = "Margaritaville - Suntan Oil Application!";
echo "Time for $my_string";
?>
Chapter 3 Code • (0) Comments • (3647) Trackbacks • Permalink
Example 03-13 Single quotes used in a string assignment
<?php
$my_string = 'Margaritaville - Suntan Oil Application!';
echo $my_string;
?>
Chapter 3 Code • (26) Comments • (150) Trackbacks • Permalink
Example 03-14 Various special characters in string assignments
<?php
$newline = "A newline is \n";
$return = "A carriage return is \r";
$tab = "A tab is \t";
$dollar = "A dollar sign is \$";
$doublequote = "A double-quote is \"";
?>
Chapter 3 Code • (3) Comments • (278) Trackbacks • Permalink
Example 03-15 Using echo with special characters
<?php
// This won't work because of the quotes around specialH2!
echo "<h2 class="specialH2">Margaritaville!</h2>";
?>
Chapter 3 Code • (2) Comments • (2037) Trackbacks • Permalink
Example 03-16 Correct escaping of special characters
<?php
// OK because we used single quotes
echo "<h2 class=\"specialH2\">Margaritaville!</h2>";
echo '<h2 class="specialH2">Margaritaville!</h2>';
?>
Chapter 3 Code • (27) Comments • (163) Trackbacks • Permalink
Example 03-17 Using strcmp to compare two stings
<?php
$name1 = "Bill";
$name2 = "BILL";
$result = strcasecmp($name1, $name2);
if (!$result){
echo "They match.";
}
?>
Returns:
They match.
Example 03-18 Concatenating strings together
<?php
$my_string = "Hello Max. My name is: ";
$newline = "<br />";
echo $my_string . "Paula" . $newline;
echo "Hi, I'm Max. Who are you? " . $my_string . $newline;
echo "Hi, I'm Max. Who are you? " . $my_string . "Paula";
//The last line is the same as echo "Hi, I’m max. Who are you? $my_string Paula";
?>
Chapter 3 Code • (27) Comments • (4) Trackbacks • Permalink
Example 03-19 Combining a string and a number
<?php
$str = "This is an example of ". 3 ." in the middle of a string.";
echo $str;
?>
Displays:
This is an example of 3 in the middle of a string.
Example 03-20 Using a constant in your program
<?php
define("HELLO", "Hello world!");
echo HELLO; // outputs "Hello world."
?>
Outputs:
Hello world!
Chapter 3 Code • (0) Comments • (45) Trackbacks • Permalink
Statistics
This page has been viewed 375212 times
Page rendered in 0.2414 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: 8
Total anonymous users: 0
Most Recent Visitor on: 02/10/2012 09:43 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
