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 […]
Category: PHP
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}; […]
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: = = […]
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’]} […]