Category Archives: How to Code PHP

Web Design and Programming Pt 13

Help Web Design and ProgrammingIn this PHP video tutorial I’ll show you how to work with exception handling.

When an error occurs in your code this code is referred to as an exception. Meaning the code runs fine Except if the user of the code does certain things. When these Exceptions occur an error is thrown and handled by code that lies somewhere else in your code.

I specifically cover the following topics in the video:

Continue reading Web Design and Programming Pt 13

Web Design and Programming Pt 12

Web Design and ProgrammingIn this PHP video tutorial I’ll cover how to manipulate files with PHP. Specifically I cover the following:

  • Read file into an array
  • Read file into a string
  • Grab text off of web pages
  • Strip HTML tags
  • Write to files

Here is a link to the first article and video for this series, if you haven’t seen it Web Design and Programming.

All of the code follows the video. Use it in anyway. Leave any questions or comments below.

Code from the Video

<html>
<head>
<title><?php echo “Object Oriented Programming”;?></title>
</head>

<body>

<?php

# Creates a file descriptor and assigns it to $file
# The second attribute opens the file for read only purposes
# You can access any file online with this same function
# r+ – Read and Write from beginning of file
# w – Write only to beginning of file
# w+ – Delete contents and read and write to file
# a – Write only to the end of the file
# a+ – Read and Write to the end of the file
$file = fopen(“customers.txt”, “r”);

# While the end of file hasn’t been met print lines to screen
while (!feof($file))
{
# fgets() returns all the characters up to a newline or EOF
# You can also specify the number of characters to return in the 2nd attribute
echo fgets($file). “<br />”;
}

echo “<br />”;

# You can also read a file into an array
$customers = file(“customers.txt”);
foreach ($customers as $customer)
{
list($name,$street,$city,$state) = explode(“,”, $customer);
$state = trim($state);
echo “Name: ” . “$name <br />”;
echo “Street: ” . “$street <br />”;
echo “City: ” . “$city <br />”;
echo “State: ” . “$state <br /><br />”;
}

# Close the file
fclose($file);

$file4 = fopen(“customers.txt”, “a”);

$newCustomer = “\nRick,219 Almond St,Pittsburgh,PA”;
fwrite($file4, $newCustomer);

fclose($file4);

# You can also read a file into a string
$customers = file_get_contents(“customers.txt”);
$customers = explode(“\n”,$customers);

foreach ($customers as $customer)
{
list($name,$street,$city,$state) = explode(“,”, $customer);
$state = trim($state);
echo “Name: ” . “$name <br />”;
echo “Street: ” . “$street <br />”;
echo “City: ” . “$city <br />”;
echo “State: ” . “$state <br /><br />”;
}

$file2 = fopen(“http://www.wikileaks.ch/static/html/faq.html”, “rt”);

while (!feof($file2))
{
$faqs .= fgetss($file2, 1024);
}
fclose($file2);

$file3 = fopen(“justthefaqs.txt”, “wt”);
fwrite($file3, $faqs);

fclose($file3);

?>

</body>
</html>

Web Design and Programming Pt 11

Object Oriented ProgrammingHere I continue to explain Object Oriented Programming with PHP. If you missed the first part definitely watch that video tutorial first Object Oriented Programming with PHP.

Here I’ll cover the following topics:

  • Inheritance
  • Static Attributes (Variables)
  • The _destruct Function
  • The Final Keyword
  • Overriding Functions
  • And more…

If you have any questions, leave them below. All of the code follows the video.

All the Code from the Video

<html>
<head>
<title><?php echo “Object Oriented Programming”;?></title>
</head>

<body>

<?php

# Inheritance is done when you create a new class from another, thus borrowing the data
# and methods that can be found there.

class Animal
{
private $name; # Private variables are only accessed by class methods, but not subclasses
private $favFood = “meat”;

# A static attributes value is shared by all objects of the class
# You refer to static attribute like this self::$numOfAnimals, not with $this
public static $numOfAnimals = 0;

# Constructor and Deconstructor
public function __construct($name=”No Name”)
{
echo “__construct was called”. “<br />”;
$this->setName($name);
self::$numOfAnimals++;
}

public function __destruct()
{
echo “__destruct was called”. “<br />”;
}

# Get and Set Methods – final is used to keep methods from being overwritten
final public function getName()
{
return $this->name;
}

final public function setName($sentName)
{
$this->age = $sentName;
}

# Animal Default Functions
public function makeNoise()
{
echo “Grrrr”. “<br />”;
}
public function favFood()
{
echo “My favorite food is ” . $this->favFood . “<br />”;
}
public function move()
{
echo “Walk around”. “<br />”;
}

}

class Dog extends Animal
{
# If I didn’t create a new constructor the parent would be called
public function __construct($name=”No Name”)
{
parent::__construct($name); # How to call the parent constructor
# Animal::__construct($name); this would also call the parent constructor
}

public function __destruct()
{
echo “__destruct was called”. “<br />”;
}

# Dog Default Functions Overwrites Class Function
public function makeNoise()
{
Animal::makeNoise(); # How to call the original class method
echo “Bark, Bark”. “<br />”;
}
}

#function animalStuff(

$grover = new Dog(“Grover”); # Create an object grover of class Dog
$paul = new Dog(“Paul”);
$grover->makeNoise(); # Call the makeNoise() method for the grover object
$grover->favFood();
$grover->move();

echo Animal::$numOfAnimals. “<br />”; # Prints out the number of objects created

?>

</body>
</html>

Web Design and Programming Pt 10 OOP & PHP

Object Oriented ProgrammingIn this PHP video tutorial I cover Object Oriented Programming with PHP. I specifically go over the following topics:

  • Encapsulation
  • Classes
  • Objects
  • Public
  • Private
  • Constructors
  • Destructors
  • __set
  • __get
  • And more….

All of the code from the tutorial is available after the video. Use it in anyway that you like. I’ll continue covering OOP with PHP in the next video tutorial in this series.

If you have any questions leave them below.

All the Code from the Video

<html>
<head>
<title><?php echo “Object Oriented Programming”;?></title>
</head>

<body>

<?php
# Encapsulation lets you hide the code behind an interface so you can protect the code
# and so your user need not understand the code to use it

# A class is a blueprint you use to create objects

# Objects contain the data (attributes) and actions (methods) needed to perform its tasks

# Inheritance is done when you create a new class from another, thus borrowing the data
# and methods that can be found there.

# Polymorphism allows a class to perform differently based on how it is being used.
# Polymorphism can be used to a certain extent with PHP in that you can use common
# method and attribute names for similar classes

# Any use of this is a reference to a specific object and that objects attributes and methods

class Animal
{
public $name; # Public variables can be accessed directly by anyone
private $age; # Private variables are only accessed by class methods, but not subclasses
protected $money; # Protected is like private except can be accessed by inherited classes
const POUNDID = 12345; # A constants value can never change
# A static attributes value is shared by all objects of the class
# You refer to static attribute like this self::$numOfAnimals, not with $this
private static $numOfAnimals = 0;

public function __construct($age)
{
echo “__construct was called”. “<br />”;
$this->setAge($age);
}

public function __destruct()
{
echo “__destruct was called”. “<br />”;
}

# __set can be used to perform error checking before assigning values to private attribs
function __set($attribName, $attribValue)
{
echo “__set was called for “. $attribName. “<br />”;
$this->$attribName = $attribValue;
}

# __get can be used to perform error checking before returning values from private attribs
function __get($attribName)
{
echo “__get was called for “. $attribName. “<br />”;
return $this->$attribName;
}

public function getAge()
{
return $this->age;
}

public function setAge($sentAge)
{
$this->age = $sentAge;
}
}

$dog = new Animal(8); # Create an object dog of class Animal
$dog->name = “Grover”; # Assign a value to the public attribute name
$dogName = $dog->name; # Retreive value from public attribute name

$dog->setAge(8); # Call the setAge() method for the dog object

$dog->age = 9; # Try to set a private attribute that makes a call to __set
echo $dog->age. “<br />”; # Try to get a private attribute that makes a call to __get

echo “The dogs name is ” . $dogName . “<br />”;
echo “The dogs age is ” . $dog->getAge() . “<br />”;
echo “The pounds ID number is “. $dog::POUNDID. “<br />”;

?>

</body>
</html>

Web Design and Programming Pt 9 PHP Strings

StringsHere I show you how to use many string manipulation methods in PHP. Also, I finish showing you how to use regular expressions.

You should watch the previous 2 PHP Regular Expressions video tutorials before you continue on to this one. They are avaialable here PHP Regular Expressions & PHP Regular Expressions Pt 2

I specifically cover the following PHP Methods in this tutorial:

Web Design and Programming Pt 7 REGEX

Regex TutorialIn this video tutorial I will tackle Regular Expressions. People think Regular Expressions are too hard to use. And, while I have received many thank you’s for creating my last Regex Tutorial. Here I’m going to finish up where I left off.

Before you proceed you should most definitely take a look at those Web Design and Programming tutorials that came previous to this one. This tutorial starts with this article Web Design and Programming.

Continue reading Web Design and Programming Pt 7 REGEX

Web Design and Programming Pt 6

iPhone DevelopmentIn this part of the Web Design and Programming tutorial, I will show you how to do anything with PHP functions. Specifically I will cover:

  • How to Create Functions
  • How to work with Function Attributes (Variables)
  • How to Pass Multiple Attributes to and from Functions
  • What Does Scope Mean (Local vs. Global)
  • What is Recursion

Continue reading Web Design and Programming Pt 6

Web Design and Programming Pt 5 PHP Arrays

Go Daddy Grid HostingIncrease Website SpeedIn part 5 of my Web Design and Programming tutorial I will cover the following topics:

  • How to turn a string into an array
  • How to turn an array into a string
  • How to sort an array
  • How to pull data from a file and store it in an array

And, I’ll also go over a bunch of other functions specific to arrays. If you missed part 1 of this tutorial it is available here Web Design and Programming.

All of the code in this tutorial will follow the video. Use it in any way that you would like.

If you have any questions or comments leave them below and I’ll answer them.

Code from the Video

<html>
<head>
<title><?php echo “Arrays”;?></title>
</head>

<body>

<?php
# An array is just a big box containing many similar boxes
# This information is similar to each other
# All the the information or values have a coresponding key or label associated with it
# An array can contain any combination of numbers, strings or other arrays

# Create basic array with key value pairs
/* $myInfo = array(“Name” => “Derek”, “Street” => “123 Main St”, “City” => “Pittsburgh”);

# Get the value by calling for the key associated with it
echo “My name is “, $myInfo[“Name”], “<br /><br />”;
$moreInfo = array(“State” => “PA”, “Age” => 35);

# Merge 2 arrays into one
$myInfo = array_merge($myInfo, $moreInfo);

# Iterate through the array with the foreach looping device
foreach( $myInfo as $key => $value)
{
echo $key, ” “, $value, “<br />”;
}
echo “<br /><br />”;

# Search for a key in an array
if(array_key_exists(“Name”, $myInfo)) echo “The name stored is “, $myInfo[“Name”];
echo “<br /><br />”;

# Search for a value in an array
$citySearch = array_search(“Pittsburgh”, $myInfo);
echo “The key for the city is “, $citySearch;
echo “<br /><br />”;
print_r (array_keys($myInfo)); # Short cut to provide the arrays keys
echo “<br /><br />”;
print_r (array_values($myInfo)); # Short cut to provide the arrays keys & values
echo “<br /><br />”;

# Create a Multidimensional Array
$customer1 = array(“Name” => “Derek”, “Street” => “123 Main St”, “City” => “Pittsburgh”);
$customer2 = array(“Name” => “Sally”, “Street” => “213 Grant St”, “City” => “Pittsburgh”);

$customers = array($customer1,$customer2);
print_r(array_values($customers));
echo “<br /><br />”;

foreach( $customers as $key)
{
foreach( $key as $key2 => $value)
{
echo $key2, ” “, $value, “<br />”;
}
} */

$countryStr = “Cuba,Spain,India,France,Italy”;
$randCountry = explode(“,”, $countryStr);
echo $randCountry[0], ” “, $randCountry[1], “<br /><br />”;

$countryStr2 = implode(“,”, $randCountry);

echo $countryStr2, “<br /><br />”;

if(in_array(“India”, $randCountry)) echo “India is in the list”;
echo “<br /><br />”;

print_r(array_reverse($randCountry,true));
echo “<br /><br />”;

sort($randCountry, SORT_STRING);
print_r($randCountry);
echo “<br /><br />”;

$countArray = range(0,50);
foreach($countArray as $printNum)
{
echo $printNum, “, “;
}
echo “<br /><br />”;

echo count($countArray);
echo “<br /><br />”;

$customers = file(“customers.txt”);
foreach($customers as $customer)
{
list($name,$street,$city,$state) = explode(“,”,$customer);
$state = trim($state);

echo “$name $street $city $state”;
echo “<br /><br />”;
}
?>

</body>
</html>

Web Design and Programming Pt 3 Looping

Objective C LoopingIn this web design and programming video tutorial I’ll show you how to use looping statements with PHP. If you missed the first part of this tutorial it is here Web Design and Programming.

I specifically will cover the following topics:

  • Using the $_REQUEST array to pass form data
  • The While Loop
  • The Do While Loop
  • The For Loop
  • How Break and Continue work

Continue reading Web Design and Programming Pt 3 Looping

Web Design and Programming Pt 2 PHP & HTML

Help Web Design and ProgrammingHere I continue covering Web Design and Programming using all of the most used languages being HTML, CSS, JavaScript, PHP, MySQL, etc. If you missed my first video, you should watch that first Web Design and Programming part 1.

In this specific tutorial I’ll cover the following:

  • How to access html form data with PHP
  • How to use if conditional statements in PHP
  • How to use the Switch statement in PHP
  • And more…

Continue reading Web Design and Programming Pt 2 PHP & HTML

Web Design And Programming Pt 1 PHP

Web Design and ProgrammingI have received tons of requests for tutorials. So, I’ve decided to create a massive tutorial that answers them all. In this tutorial I’ll focus on Web Design and Programming using all of the tools required. You should check out my Learn HTML Tutorial if you haven’t ahead of time.

I’ll continue this tutorial, with your input, until you can do pretty much anything in Web Design and Programming. Leave questions and comments below, because you are going to totally control everything I cover here.

I’ll start off with the basics of PHP and move on from there with tons of examples. Of course, all of the code used will follow the video.

All the Code from the Video

<html>
<head>
<title><?php echo “Website Title”;?></title>
</head>
<body>
<?php

// You can use short tags but it is not recommended doesn’t work in 6
# You can also comment code like this
/* Here is a
multiline comment
*/
echo “<p>Random Text</p>”;
print(“<p>You can print with braces</p>”);
print “<p>or without
them!</p>”;

$randVar = “Cats”;
echo “<p>”, $randVar, ” are funny</p>”;

printf(“<p>My name is %s I’m %d years old and
pi is equal to %.2f</p>”, ‘Derek’, 35, 3.14);

// Variables must start with a $ then letter or _ and then numbers
// Variables are case sensitive & don’t require definition before use

$boolEx1 = false; // 0 or false equals false
$boolEx2 = 1; // true equals any number but 0

$intEx1 = -234; // Max size normally 2^31 or 2^63

$floatEx1 = 3534.14;

echo “<p>Cost: $”,number_format($floatEx1,2), “</p>”;

// Arithmetic Operators: + – * / % ++ —

$firstName = “Doctor”;
$lastName = “Who”;

$wholeName = $firstName . ‘ ‘ . $lastName;
printf(“<p>His name is %s</p>”,$wholeName);

define(‘ACONSTANTVAR’, 2345);

echo “A constant is equal to ” . ACONSTANTVAR . “<br />”;

echo “How do quotes differ $wholeName<br />”;
echo ‘How do quotes differ $wholeName<br />’;

/* Escaped Characters in double quotes \” \’ \\ \n \r \t \$ */

$float2Int = (int) $floatEx1;
echo $float2Int, “<br />”;

/*
(array)
(bool)
(int64)
(object)
(float)
(double)
(string)
*/

$strNum = “28”;
echo $strNum * $floatEx1, “<br />”;

echo gettype($strNum), “<br />”;
echo is_string($strNum), “<br />”;

/* is_array() is_bool() is_float() is_integer() is_null() is_object()

?>
</body>
</html>

How to Code PHP Scripts Video Tutorial

In this series of video’s I take you to the next level in learning How to Code PHP. I’ve included many PHP tutorials on the most in demand PHP scripts including:

  • How to Code a PHP Simple Login Script
  • How to Code a PHP New User Registration Script
  • How to Code a PHP Quality Login Script
  • How to Code a PHP Forgotten Password Script
  • How to Code a PHP Account Activation Script
  • How to Code a PHP Automatic Updating Multiple Drop Down Boxes Script Continue reading How to Code PHP Scripts Video Tutorial

How to Code PHP: Activate Account & Multiple Drop Down Scripts

Here I’ll show you the code that will allow a new user to activate their account and how to create multiple drop down boxes that automatically update. This should be a wild ride so let’s learn How to Code PHP.

PHP Activate Account Script

Obviously, you are going to need to connect to the database and include all of the HTML needed. See my first How to Code PHP Tutorial for that information. I’m just going to include the PHP code for the activation script to keep the article brief.

Here I’ll show you the code that was used on the New User Registration page. This code created the account and then sent the email, that the user will click to get to your activation page. Continue reading How to Code PHP: Activate Account & Multiple Drop Down Scripts

How to Code PHP Sessions & Cookies

In this article I’m going to talk about using cookies and sessions in PHP. I’ll provide many examples to help you learn how to code PHP and then in the next article run you threw some PHP code you can use on your own sites.

Cookies & Sessions

There are two main ways for you to store information on your visitors and create a dynamic site. Cookies are used to store information on the visitors computer. Much like you do with arrays, cookies are stored as a key value pair.

Many people are afraid of cookies, because they think those websites that use them are tracking their every move. For this reason it may be hard to serve up the dynamic content that you want. There are work arounds for those visitors that have cookies shut off as you’ll soon see. Continue reading How to Code PHP Sessions & Cookies

How to Code PHP Part 2

Welcome to the second part of the How to Code PHP Tutorial. In this article I’ll show you the rest of the basics of using the PHP Engine. I’ll cover the following:

  • IF Statements
  • SWITCH Statements
  • FOR Loops
  • WHILE Loops
  • DO WHILE Loops
  • Including Files
  • Creating Functions
  • What Does Scope Mean?
  • Date and Time Functions
  • How to Send Email
  • Accessing Databases

Continue reading How to Code PHP Part 2