Learning PHP
<?php
$username = "Tom";
echo $username
echo "<br>";
$current_user = $username
echo $current_user;
$y = 0;
if( $y-- = 0)
echo $y;
echo "This is $y--";
if( --$y = 0)
echo $y;
echo "This is $y--";
// String concatenation
$msg = 5
echo "You have " . $msg . "messages.";
// You have 5 messages.
$var = "some"
$var .= "body"
//somebody
// Test Difference
$info = 'Preface variable with a $ like this: $variable';
echo $info;
// Variable substitution
// In this example you don't need to use the . to concatenate the string
echo "This week $count people have viewed your profile";
// Escaping characters
$text = 'My spelling\'s still sucks';
echo $text;
$text = "She wrote \"I don't what to have a pony\".";
echo $text;
// Special escape only can happen in "", double quotes
$text = "Date\tName\tPayment\nNewLine:\rR_something";
echo $text;
// Won't work
//Only \' and \\ work in '', single quotes
$text = 'Date\tName\tPayment\nNewLine:\rR_something';
echo $text;
echo "This is
the longest
line
ever";
/*
echo <<<_END
This is
the longest
line
ever
_END;
*/
$number = 12345 * 67890;
echo substr($number, 3, 1);
function longdate($timestamp){
// return date("l F js Y", $timestamp);
$temp = date("l F js Y", $timestamp);
return "The date is $temp";
}
echo longdate(time());
// Print out date from 17 days ago
echo longdate(time()-17*24*60*60);
function static_varTest(){
/// This will be skipped next time function is called
static $count = 0;
echo $count;
$count++;
}
// The URL of the page that referred the user to the current web page. This referring page information can be accessed like this
// PHP htmlentities function. It converts all characters into HTML entities to protect your code from hackers
$came_from = htmlentities($_SERVER['HTTP_REFERER']);
// Switch statement
switch ($page)
{
case "Home":
echo "You selected Home";
break;
case "About":
echo "You selected About";
break;
case "News":
echo "You selected News";
break;
case "Login":
echo "You selected Login";
break;
case "Links":
echo "You selected Links";
break;
default:
echo "Unrecognized selection";
break;
}
// While Loop
$count = 0;
while (++$count <= 12)
{
echo "$count times 12 is " . $count * 12 . "<br>";
$count;
}
// do while Loop
do
{
echo "$count times 12 is " . $count * 12;
echo "<br>";
}
while (++$count <= 12);
// for Loops
for ($count = 1 ; $count <= 12 ; ++$count)
{
echo "$count times 12 is " . $count * 12 . "<br>";
}
// Reverse string
echo strrev(" .dlrow olleH");
// Repeat string
echo str_repeat("Hip ", 2);
// String to uppercase
echo strtoupper("hooray!");
// String to lowercase
echo strtolower('CARLO');
// String uppercase first letter
echo ucfirst(('carlo nyte');
echo fix_names("WILLIAM", "henry", "gatES");
function fix_names($n1, $n2, $n3)
{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));
return $n1 . " " . $n2 . " " . $n3;
}
// Passing by Reference
$a1 = "WILLIAM";
$a2 = "henry";
$a3 = "gatES";
echo $a1 . " " . $a2 . " " . $a3 . "<br>";
fix_names($a1, $a2, $a3);
echo $a1 . " " . $a2 . " " . $a3;
function fix_names(&$n1, &$n2, &$n3)
{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));
}
// Returning Global Variables
$a1 = "WILLIAM";
$a2 = "henry";
$a3 = "gatES";
echo $a1 . " " . $a2 . " " . $a3 . "<br>";
fix_names();
echo $a1 . " " . $a2 . " " . $a3;
function fix_names()
{
global $a1; $a1 = ucfirst(strtolower($a1));
global $a2; $a2 = ucfirst(strtolower($a2));
global $a3; $a3 = ucfirst(strtolower($a3));
}
// Declaring a Class
$object = new User;
print_r($object);
class User
{
public $name, $password;
function save_user()
{
echo "Save User code goes here";
}
}
// Creating and interacting with an object
$object = new User;
print_r($object); echo "<br>";
$object->name = "Joe";
$object->password = "mypass";
print_r($object); echo "<br>";
$object->save_user();
class User
{
public $name, $password;
function save_user()
{
echo "Save User code goes here";
}
}
// Cloning an object
$object1 = new User();
$object1->name = "Alice";
$object2 = clone $object1;
$object2->name = "Amy";
echo "object1 name = " . $object1->name . "<br>";
echo "object2 name = " . $object2->name;
class User
{
public $name;
}
// Creating a constructor method in PHP 5
class User
{
function __construct($param1, $param2)
{
// Constructor statements go here
public $username = "Guest";
}
}
// Creating a destructor method in PHP 5
class User
{
function __destruct()
{
// Destructor code goes here
}
}
// Using the variable $this in a method
class User
{
public $name, $password;
function get_password()
{
return $this->password;
}
}
// Creating and accessing a static method
User::pwd_string();
class User
{
static function pwd_string()
{
echo "Please enter your password";
}
}
?>