PHP Tutorial -2 (Data type and array)

PHP tags
<?php ?>
Text Output
echo "something";
Variable
echo "something"; //$ in front, can not begin with a number, case sensitive
Constant
define("name", "value");
Format number
echo sprintf("%01.2f", 25); //25-->25.00
echo number_format(25000, 2); //25000-->25,000.00

Single-quoted Vs double-quoted strings
$age=12; $result1="age"; $result2='age'; //resut1:12 resutl2: $age
Joining string, or concatenation (dot . or .=). To get Hello World!
echo 'Hello'." ".'World';
Format a date ( August 10, 2006 –> 2006/08/10)
echo date("Y/m/d");
Array-Create. index starts with 0, use one of the following ways to create.
$pets[0]="unicorn"
$pets = array("dragon", "unicorn", "tiger");
$pets[] ="dragon"; $pets[] ="unicorn"; $pets[] ="tiger";
$capitals =array("CA" => "Sacramento", "TX" => "Austin", "OR" => "Salem" );
Array-Viewing.
print_r($pets);
var_dump($pets);
Array-Removing.
$pets[2]="";
unset ($pets[4]);
Array-Sorting.
sort("pets"); //To sort an array that has numbers as keys and reassign keys
asort("pets"); //To sort an array that has strings as keys
Array-Getting Values.
list ($firstvar, $secondvar)=$pets;
//To get value from an array that has numbers as keys and put them in variables
extract("pets");
//To copy all the values from an array into variables named from the corresponding keys. The sample from the book is wrong, since no key was assigned in $shirtInfo
current($arrayname) next($arrayname) previous($arrayname) end($arrayname) reset($arrayname);
//Manually iterate through an array
foreach($capitals as $state =>$city)
//walks through the array one value at a time.

PHP Tutorial -1

PHP & MySQL for Dummies

This is the book I am going to start with learning PHP. I am using MS SQL server at work, so MySQL part is not a problem. The basic PHP commands and functions will be listed along with my study progress.

Chapter 2: Connect to mysql

The provided mysql_up.php used mysqli_query function, which due to unknown reason does not work. MySQL revision at my website is 5.1.30 verified with mysql_get_server_info. But mysql_up-4.php works, it used mysql_query.

Check MySQL connection

<?php
/* Program: mysql_up.php
* Desc: Connects to MySQL Server and
* outputs settings.
*/
echo "<html>
<head><title>Test MySQL</title></head>
<body>";
$host="fdb2.host-ed.net";
$user="username";
$password="password";
$cxn = mysql_connect($host,$user,$password);
$sql="SHOW STATUS";
$result = mysql_query($sql);
if($result == false)
{
echo "<h4>Error: ".mysql_error($cxn)."</h4>";
}
else
{
/* Table that displays the results */
echo "<table border='1'>
<tr><th>Variable_name</th>
<th>Value</th></tr>";
for($i = 0; $i < mysql_num_rows($result); $i++)
{
echo "<tr>";
$row_array = mysql_fetch_row($result);
for($j = 0;$j < mysql_num_fields($result);$j++)
{
echo "<td>".$row_array[$j]."</td>n";
}
}
echo "</table>";
}?>
</body></html>

Check MySQL revision

<?php
$host="fdb2.host-ed.net";
$user="username";
$password="password";
$link = mysql_connect($host,$user,$password);if (!$link) {
die('Could not connect: ' . mysql_error());
}
printf("MySQL server version: %sn", mysql_get_server_info());
?>

Remove WordPress Revision History

After a few day’s playing around with wordpress, I felt one thing is quite annoying. It does not matter how little change you made to your post, wp always save them by means of revision. The result is a waste of resources and unnecessary longer database query time. I really see no reason to save that many revisions. There are a couple of ways to get rid of this “feature”.

  1. SQL command:
    DELETE FROM wp_posts WHERE post_type = “revision”;
    select wp_posts table and run the command, make sure “  (double or single quote) is in proper format.
  2. Modify  wp-config.php :
    define(‘WP_POST_REVISIONS’, false);
    the file locates at the website root directory, add the code before /* That’s all, stop editing! Happy blogging. */, it turn out there is still one more thing needs to be done.
  3. Plugin
    Can not believe there is even a plug in for this, anyway this is the link.  Delete-Revision

The official document is here: Revision Management. It turn out that there is only one autosave in database and the record will be updated every 60 second. The autosave has nothing to do with post revision control. The purpose is to give you something  in case you have system crash while editing a post, I can live with that. I would like to suggest WordPress to move this from core to something like plug in.