Category Archives: PHP

PHP Class – OOP

A PHP Class can be used for several things, but at the most basic level, you’ll use classes to “organize and deal with like-minded data”. Here’s what I mean by “organizing like-minded data”. First, start with unorganized data.

<?php
$customer_name;
$item_name;
$item_price;
$customer_address;
$item_qty;
$item_total;
?>

Now to organize the data into PHP classes:

<?php
class Customer {
$name; // same as $customer_name
$address; // same as $customer_address
}

class Item {
$name; // same as $item_name
$price; // same as $item_price
$qty; // same as $item_qty
$total; // same as $item_total
}
?>

Now here’s what I mean by “dealing” with the data. Note: The data is already organized, so that in itself makes writing new functions extremely easy.

<?php
class Customer {
public $name, $address; // the data for this class…

// function to deal with user-input / validation
// function to build string for output
// function to write -> database
// function to read <- database // etc, etc } class Item { public $name, $price, $qty, $total; // the data for this class... // function to calculate total // function to format numbers // function to deal with user-input / validation // function to build string for output // function to write -> database
// function to read <- database // etc, etc } ?> Imagination that each function you write only calls the bits of data in that class. Some functions may access all the data, while other functions may only access one piece of data. If each function revolves around the data inside, then you have created a good class.

Using substr()

<?php
print substr(‘abcdef’, 1); // bcdef
print substr(‘abcdef’, 1, 3); // bcd
print substr(‘abcdef’, 0, 4); // abcd
print substr(‘abcdef’, 0, 8 ); // abcdef
print substr(‘abcdef’, -1, 1); // f

// Accessing single characters in a string
// can also be achived using “curly braces”
$string = ‘abcdef’;
print $string{0}; // a
print $string{3}; // d
print $string{strlen($string)-1}; // f

?>

Negative start

<?php
$test_var = substr(“abcdef”, -1); // returns “f”
$test_var = substr(“abcdef”, -2); // returns “ef”
$test_var = substr(“abcdef”, -3, 1); // returns “d”
?>

PHP: Expressions and Operators

Arithmetic Operators:

+ Addition Add two values
– Subtraction Subtract the second value from the first
* Multiplication Multiply two values
/ Division Divide the first value by the second
% Modulus Divide the first value by the second and return only the remainder
(for example, 7 % 5 yields 2)

Comparison Operators:

= = Equal Checks for equal values
= = = Identical Checks for equal values and data types
! = Not Equal Checks for values not equal
! = = Not Identical Checks for values not equal or not the same data type
< Less than Checks for one value being less than the second
> Greater than Checks for one value being greater than the second
< = Less than or Equal to Checks for on value being less than or equal to the second
> = Greater than or Equal to Checks for on value being greater than or equal to the second

Logical Operators:

And Checks if two or more statements are true
&& Same as And
Or Checks if at least one of two statements is true
|| Same as Or
Xor Checks if only one of two statements is true
! Checks if a statement is not true

Increment and Decrement Operators:

++value Pre-Increment Adds 1 to the value before processing the expression which uses the value
–value Pre-Decrement Subtracts 1 from the value before processing the expression which uses the value
value++ Post-Increment Adds 1 to the value after processing the expression which uses the value
value– Post-Decrement Subtracts 1 from the value after processing the expression which uses the value

eregi_replace() vs. ereg_replace()

Code:

<?php

$s = “Coding PHP is fun.”;

print “ereg_replace(): ” .
ereg_replace(“CODING”, “Learning”, $s) . “<br>”;
print “eregi_replace(): ” .
eregi_replace(“CODING”, “Learning”, $s);

?>

Output:

ereg_replace(): Coding PHP is fun.
eregi_replace(): Learning PHP is fun.
Explanation:

eregi_replace() is case insensitive, while ereg_replace() is not.

PHP basename() Function

The basename() function returns the filename from a path.

<?php
$path = “/mywebsite/home.php”;

//Show filename with file extension
echo basename($path) .”<br/>”;

//Show filename without file extension
echo basename($path,”.php”);
?>

The output of the code above will be:

home.php
home

PHP Alternating Row Colors

<?php
include(‘dbconnect.php’);
$query1 = “SELECT * FROM clients_info order by fname asc”;
$r1 = mysql_query($query1) or die(‘Error, query1 failed to retrieve information from client_info table’);

$color1 = ‘#90EE90′;
$color2 = “#CCFFCC”;
$row_count = 0;

while($row = mysql_fetch_array($r1))
{
$color = ($row_count % 2) ? $color1 : $color2;
print'<div id=”clients_name” style=”background-color:’ . $color . ‘”>’;
print”{$row[‘fname’]} {$row[‘lname’]}<br/>”;
print'</div>’;
$row_count++;
}
?>