PHP - Turning Off Error Reporting

Add the following to your PHP code:

error_reporting(5);

This will close all error reportings.

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.

Formatting money / currency using PHP

$formatted = number_format($number,2);

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++;
}
?>