##########################################
#### -------------------------------- ####
#### >>> READ ALL THE ORANGE TEXT!! ALL OF IT.. ####
#### >>> READ ALL CODE EXAMPLES... ####
#### >>> READ ALL OF IT..... !ALL! ####
#### -------------------------------- ####
#### >>>> UNDERSTANDING IS THE CLUE ####
#### >>> DO NOT SKIP A SINGLE WORD!! ####
#### >>> AND REMEMBER I TOLD YOU ABOUT IT ####
#### -------------------------------- ####
#### >>>> UNDERSTANDING IS THE CLUE ####
#### >>>> IT'S ALL THE DIFFERENCE YOU NEED ####
### >>>> TO BE AN EXPERT IN ONE OR TWO HOURS ####
## -------------------------------- ####
# ........IMAGINE ! ###########################
# HISTORY
# 0 My crew is in the total infancy of php knowledge!
# All of them. I am alone...
# 0.0.1 collecting code snippets into library.PHP
# 0.0.2 Emailing a one page email to colstaff declaring imminent courses
#
# 0.1 Outlining the principal chapters
# variables constants globals post get cookies
# GET POST SERVER REQUEST SESSION why?
# conditions ? '':'' if elseif else switch
# arrays numeric, alphanumeric indexes is_array is_key
# databases select syntax
# mysql one row retrival
# loops for while fun with loops
# mysql multirows
# function creation, call values, globals and return of multiple values
# strings substr strlen strpos str_replace ereg etc
# dates time date strtotime juliandatecount
# math functions
# 0.2 Adding meat to the bones, filling chapter by chapter with
# php compatible code
# 0.3 History created
# 0.4 Maintenance update, code cleanups and reuse
# 0.5 working on show_source code switch
# 0.6 implemented "source - result" display
# 0.7 Maintenance and cleanup
# 0.8 added more comments
# 0.9 another revision with more comments, some re-ordering
# 1.0 last revision by jonathan
$ver = "1.0";
$PHP = "PHP";
$CRASH = "CRASH";
$COURSE = "COURSE";
// I'll explain this next four lines at the end of this page
if($_REQUEST["switches"] =="on"){$sw="off";$sw2="on";}
if($_REQUEST["switches"] =="off"){$sw="on";$sw2="off";}
if($_REQUEST["switches"] ==""){$sw="on";$sw2="off";}
if(!isset($_REQUEST["countz"])){$countz=1;}else{$countz=$_REQUEST["countz"];}
// ok, lets continue . . .
$title = "
# 1 ################# INTRO #######
\n";
// THIS ONE DOES NEED THE CONCATENATING DOT as ALL FOLLOWING ONES
// IF ONE OF THE FOLLOWING $output_strings .= IS MISSING THE DOT,
// THE HTML STARTS THERE AND NOT HERE
// OK, lets start...
// PHP is a complex scripting language which originally made html pages.
// Today's ver 5 can produce all kind of other outputs like images, flash and pdf
// One of the powers of php is it's string manipulation commands. More to this later...
// But the basics havn't changed at all.
$variable_name = "Hello World";
$output_string.= "$variable_name
";
$output_string.= $variable_name . "
";
// If you want to see something, echo "it";
// or in the case of this file, $output_string.= "it!";
// everything starts with the border between html and PHP.
// A php document has some or all code in the "PHP Mode" as I call it.
// Anything HTML code echo "Anything PHP code";
// This symbol appears at the beginning and a second inverted
// one at the end of this document. Have a look at the end.
########################################################
###################### GET POST SERVER REQUEST COOKIE SESSION why?
########################################################
$output_string.= "\n
# 2 ################# GLOBALS #######
\n";
// The PHP environment has three types of variables:
// 1- GET POST REQUEST This variables are not stored and loose their
// values if not resubmitted
// 2.- COOKIE SESSION Client side and server side storage mechanisms
// Can be retireved after a page reload or on any other page.
// 3.- SERVER Provided variables from the apache server. Quite handy variables
// for bigger projects.
// This arrays are provided resources to make things happen.
// PHP lives and dies with the submissions. Before and after form
// submission are the two deciding states of a php script!
// We use them to manipulate the represented html output.
// Their Names are as follows:
// $_GET , $_POST , $_SERVER , $_REQUEST , $_COOKIE and $_SESSION
// !!! All of them are ARRAYS !!!
// Printing the global $_REQUEST["first_name"];
$output_string.= "\$_REQUEST[first_name] " . $_REQUEST["first_name"] . "
";
########################################################
##################### VARIABLES CONSTANTS GLOBALS POST GET COOKIES
########################################################
$output_string.= "\n
# 3 ################# VARIABLES #######
\n";
// This chapter is the start of your understanding of PHP
// VARIABLES are written like this: "$name" for single values or "$name[$index]" for multi value arrays
// CONSTANTS are a special form of variable. It's values can not be
// changed in the program run.
// They are more complicated to write:
define('FIXEDNUMBER', '25');
$output_string.= FIXEDNUMBER . "
";
// GLOBALS are wonderful! Access to all submitted data values!
// If you would submit a form and have a field with the label first_name,
// after a submission you can show this variable with echo $first_name;
// This variables are extracted by php from the superglobals
// $_POST $_GET $_COOKIE and all together are contained in $_REQUEST
// Instead of printing the global:
// echo $first_name;
// we could also, and that is the recommended form to do it, with:
$output_string.= "\$_REQUEST[\"first_name\"] " . $_REQUEST["first_name"] . "
";
// if not known
$output_string.= "\$_POST[\"first_name\"] " . $_POST["first_name"] . "
";
// if post method
$output_string.= "\$_GET[\"first_name\"]" . $_GET["first_name"] . "
";
// if get method
$output_string.= "\$_GET[\"alpha\"]" . $_GET["alpha"] . "
";
// again get method
$output_string.= "\$variable_name " . $variable_name . "
";
// !!! for security globals should be turned off in php.ini !!!
// Cookies are special in the sense that they stores values on the
// client's machine and not in memory on the server. Sessions store
// values too, but on the server in files in the temp directory
########################################################
######################### CONDITIONS IF ELSEIF ELSE SWITCH ? '':''
########################################################
$output_string.= "\n
# 4 ################# CONDITIONS #######
\n";
// Every progamming language has Conditions. Php has three.
// 1.- IF ELSEIF ELSE
// in the case of a fresh pageload, the variable $_REQUEST["alpha"]
// would be not set.
// so we have to provide a default value.
if(isset($_REQUEST["alpha"])){$thiz = $_REQUEST["alpha"];}else{$thiz =0;}
// !!!! ISSET checks if the key,the variable name exists.
// It does not check for a value !!!
// to check for values use if($variable_name == "something"){
// do something;
// }
// now we have a sure way to evaluate the value of the variable $thiz.
// it will contain in any case a value
// syntax:
// if(condition) { action1; } else { action2; }
if($thiz ==0){
$that = "Number is zero";
}elseif($thiz ==1){
$that = "Number is one";
}else{
$that = "Number is higher then one";
} // endif
$output_string.= $that . "
";
// if contitions are very intuitive and easy to understand
// The elseif statement permits even to check various variables in one condition
if($thiz =="0"){
$that = "Number is zero";
}elseif(isset($_GET["alpha"])){
$that = "Value alpha is " . $_GET["alpha"];
}else{
$that = "Value is higher then one";
} // endif
$output_string.= $that . "
";
// 2.- SWITCH
// syntax:
// switch( $variable){
// case "text1":
// action1;
// break;
// case "text2":
// action2;
// break;
// case "234":
// action3;
// break;
// etc...
// case default:
// action3;
// break;
// } // endswitch
// and a real example
if(condition) { action1; } else { action2; }
switch ($_REQUEST["alpha"]) {
case "increment":
$countz++;
$output_string.= "POST Submit counter $countz
\n";
break;
case "reset_it":
$countz=0;
break;
default:
$output_string.= "
# 11 ################# STRINGS #######
\n";
// some test text string with an archival number at the end
$string = "Text in Ohio. 14567";
$output_string.= "ORG $string ----searching-------";
// find . position count in string
$pos = strpos($string,".");
$output_string.= "found the dot at string position " .$pos . "
";
// substring from the start to pos
$text = substr($string,0,$pos);
// get the last five digits of the string
$number = substr($string,-5);
// add a
in betweeen
$new_string = "both variables with a break in between
" . $text . "
" . $number;
$output_string.= $new_string . "
";
// does not need to be the first cahacter to start from
$start_pos = 5;
$end_pos = 8;
$text = substr($string,$start_pos,$end_pos);
$output_string.= "from 5th to 8th character: " . $text . "
";
// replace something with something else
$string = str_replace("in","AT",$text);
$output_string.= "Text replace " . $string . "
";
// or the more complicated function
$string = eregi_replace("at","IN",$string);
$output_string.= "Text replace " . $string . "
";
// regular expressions (scary stuff)
if(ereg("PHP_Crash_Course.php",$_SERVER["PHP_SELF"])){
$output_string.= "HOME
";
}
// ereg_replace is case_sensitive
// eregi_replace is NOT case_sensitive
// use str_replace if not needed for lower or upper case comparison
$string = str_replace("at","in",$string);
// CONCATENATION OF STRING VARIABLES
$text1 = "Antoinette";
$text2 = "Brigitt";
$text3 = "Claudia";
$text4 = "Denise";
$a=5;
$textz1[$a] ="ZEUS";
$textz2[$a] ="Poseidon";
$textz3[$a] = "Hermes";
$textz4[$a] = "Apollo";
//no spaces
$value = $text1 . $text2 . $text3 . $text4;
// with spaces
$value = $text1 . " " . $text2 . " " . $text3 . " ". $text4;
$output_string.= $value . "
";
// all together
$value = "$text1 $text2 $text3 $text4";
// arrays
$concat_value = " " . $textz1[5] . " " . $textz2[5] . " " . $textz3[5] . " " . $textz4[5];
$output_string.= $concat_value . "
";
// all together
$output_string.= $concat_value . " <=> " . $value;
// QUOTES "" ''
// DOUBLE INSIDE DOUBLE NEEDS ESCAPING
$output_string.= "
This buttons are POST submissions
\n";
$output_string.= "
This link is a GET submission
\n";
$output_string.= "$addon_html A Link to test \"GET\" submissions
\n";
// Check the submit results in Loops below and Variables above!
$output_string.= "
Check the submit results in Loops below and Variables above!
\n";
// make an enter in the document writing
$output_string.= "\n";
$html = "
!!!! Put this (slash-r slash-n) at the end of ";
$html.=" every meaningful html block.\n In view code of your browser ";
$html.="you will see actual enters after html blocks";
$html.=" for example. # 5 ################# ARRAYS ####### # 8 ################# LOOPS ####### # 6 ################# DATABASE THEORY ####### # 7 ################# MYSQL single row retrival ####### # 9 ################# MYSQL multi row retrival ####### # 10 ################# FUNCTIONS ####### # 12 ################# DATE TIME ####### $nowdate # 13 ################# MATH ####### # 14 ################# FUNCTION LIBRARY ####### # 15 ################# BROWSE ####### # 16 ################# S T R U C T U R E ####### # 17 ################# INCLUDES ####### # 18 ################# PARSE_INI ####### # 19 ################# CALI ####### # 20 ################# FILE AND DIRECTORIES ####### #@%&*@# the explaining of the file() function !!!
\n";
$html.=" DO IT! Right click and view code.\n";
$output_string.= $html;
// \n is a windows enter
// \n is a linux enter
// \r is a mac enter
// you pick the
\n";
// Arrays are a convenient way to store a lot of data in an ordered
// manner. Arrays consist of a name and a key on one side and the
// value on the other side of the equal sign.
// Array syntax:
// $name [ "index_key1" ] = $value;
// $name [ "index_key2" ] = $value;
// $name [ "index_key3" ] = $value;
// single dimensional arrays
$room=array('1','2','3','4','5','6','7');
$room=array('1'=>'1','2','3','4','5','6','7');
// The difference is that the first array's index is zero
// and the second array's first index is one
// !!! Array counts allways start with zero if not specified different!!!
// multi level arrays
$room2=array('1','2','3');
$room=array('1','2','3','4','5','6',$room2);
// This are arrays with ascending numeric keys.
// we could also have created the arrays like this
$room[] = 1;
$room[] = 2;
$room[] = 3;
$room[] = 4;
$room[] = 5;
$room[] = 6;
$room[] = 7;
// and for the second case
$room[1] = 1;
$room[] = 2;
$room[] = 3;
$room[] = 4;
$room[] = 5;
$room[] = 6;
$room[] = 7;
// after specifying the first index, the rest of the indexes will be
// created automatically.
// Here we see an array with alphanumenric keys:
$room['one'] = 1;
$room['two'] = 2;
$room['three'] = 3;
$room['four'] = 4;
$room['five'] = 5;
$room['six'] = 6;
$room['seven'] = 7;
// or
$room = array('one'=>1,'two'=>2,'three'=>3,'four'=>4,'five'=>5,'six'=>6,'seven'=>7);
// you could even have it mixed
$room['one'] = 1;
$room['two'] = 2;
$room[123] = 3;
$room[88] = 4;
$room['five'] = $room2;
$room['CE234a'] = 6;
$room['000.001'] = 7;
// lest visualize the array
// Dont sweat over the details of this, I explain it later in loops
while(list($label,$value_name) = each($room)){
if(is_array($value_name)){
// loop value_name array
for($x=0;$x
";
} // end is array
} // endwhile
// looks worse then it is, doesn't it?.
// I will explain this in detail later in the loop chapter.
// But it always comes down to this
$arraylabel[$keylabel] = $value;
// to show a single array value, write the key inside the brackets.
$output_string.= "Single array key access:
";
$output_string.= $room['CE234a'] . "
";
$output_string.= $room['two'] . "
";
########################################################
################################## LOOPS FOR WHILE FUN WITH LOOPS
########################################################
// Loops are the bread and butter of programming. They let the computer
// do the work...Use them as often as possible
$output_string.= "\n
\n";
// any numeric ascending key arrays can be displayed fast like this:
$output_string.= "print a numeric array:
";
for ($counter=0; $counter < count($room2); $counter++){
$output_string.= "$counter = " . $room2[$counter] . "
";
} //end for
// for alphanumenric array keys we need the while loop
$output_string.= "
print the alpha numeric REQUEST array
";
while(list($label,$value_name) = each($_REQUEST)){
$output_string.= "$label = $value_name
";
}
// LETS PLAY WITH COUNTING UP
$output_string.= "
loop to 255: ";
for ($counter=0;$counter<255;$counter++){
$count = $counter;
$output_string.= "$counter ";
} //end for
// LETS PLAY WITH COUNTING DOWN
for ($counter=254;$counter>=0;$counter--){
$count = $counter;
$output_string.= "$counter ";
} //end for
$output_string.= "
and back to zero.
";
// you always can skip an array element with continue
// or exit a loop with break
// do it before the output concatenation
$output_string.= "
Start with 5, Skip 10 and stop at 15: ";
for ($counter=5;$counter<255;$counter++){
$count = $counter;
// skip count 10
if($counter==10){continue;}
// exit loop with 15
if($counter==16){break;}
// concatenate all others numbers
$output_string.= "$counter ";
} //end for
########################################################
##################################### DATABASES SELECT SYNTAX
########################################################
$output_string.= "\n
\n";
// Databases are big arrays!
// in the most simple form a databse row would be something like this:
$row0["column_name"] = "value";
// this would be one column of a row
// a whole row would have more then one key label
$row0['first_name'] = "Joe";
$row0['last_name'] = "Sixpack";
$row0['email'] = "Joe@sixpack.com";
$row0['fax'] = "0019875555";
$row0['address'] = "crater 15";
$row0['city'] = "cancun";
$row0['zip'] = "77500";
// A simple array with several keys
// Now, databases have tens of thousands of rows....
// To handle that organized, we need to add another dimension to our
// two dimensional array, a row counter
$rowz["$index"]["column_name"] = "$value";
// multiple keys and columns
$row[0]['first_name'] = "Joe";
$row[0]['last_name'] = "Sixpack";
$row[0]['email'] = "Joe@sixpack.com";
$row[0]['fax'] = "0019875555";
$row[0]['address'] = "crater 15";
$row[0]['city'] = "cancun";
$row[0]['zip'] = "77500";
$row[1]['first_name'] = "Joanne";
$row[1]['last_name'] = "Whiskey";
$row[1]['email'] = "Joanne@wiskey.com";
$row[1]['fax'] = "00196774443";
$row[1]['address'] = "tulum av 12";
$row[1]['city'] = "cancun";
$row[1]['zip'] = "77500";
$row[2]['first_name'] = "etc....";
// show this array construct
// numeric loop
for($rowindex=0;$rowindex
";
} // endwhile
} // end for
$output_string.= "
$rowindex rows returned
\n";
$output_string.= "Looks like Database output, doesn't it?
\n";
// to access a database table we need to specify two paramenters:
// A.- Which columns we want to see
// and
// B.- What limiters we want to apply.
/*
########################################################
###################################### MYSQL ONE ROW RETRIVAL
########################################################
$output_string.= "\n
\n";
$output_string.= "
Well, lets do the real thing and query a database.
\n";
// SQL syntax basics:
$query = "Select column_name1, column_name2, column_name3
from tablename
where specific_column = 'filtervalue' ";
// !!!! And DON'T forget the single quotes '$var' around the filtervalues!
// MYSql won't work without them !!!
// Here is a simple query returning two columns of one row:
// connect to database
mysql_connect( "localhost", "tusr", "kiraarik") or die( "Unable to connect to SQL server");
mysql_select_db( "tusr") or die( "Unable to select database");
// query row 1234 in table contact
$id = "2102";
$table_name = "contact";
// prepare
$query="SELECT first_name,last_name from $table_name where uidc = '$id'";
// execute
$result=mysql_query($query);
// output values
$first_name = mysql_result($result,0,'first_name');
$last_name = mysql_result($result,0,'last_name');
$output_string.= "First name: " . $first_name . "
Last name: " . $last_name . "
";
########################################################
#################################### MYSQL MULTIROW RETRIVAL
########################################################
// Now that we know what the loops can do,
// lets get several rows from a table:
$output_string.= "\n
\n";
// connect to database
// !!! commented out mysql_connect( "$hostname", "$user", "$password") or die( "Unable to connect to SQL server");
// !!! commented out mysql_select_db( "$database") or die( "Unable to select database");
// already connected
// query fields in table
$resstat = 1;
$loc = "Playa";
$query="select * from cunhot WHERE res_status LIKE '$resstat' AND res_status!=0 AND max_adults!=0 AND show_where=1 AND hotel_location LIKE '$loc%' ORDER BY 'hotel_name' ASC";
$result=mysql_query($query);
// retrive total row count
$total_rows=mysql_numrows($result);
// build a while loop and start count with zero
$count=0;
while($count < $total_rows){
// get all vars for each row
$uid=mysql_result($result,$count,"uid");
$sortid=mysql_result($result,$count,"sortid");
$hotel_name=mysql_result($result,$count,"hotel_name");
$hotel_description=mysql_result($result,$count,"hotel_description");
$hotel_location=mysql_result($result,$count,"hotel_location");
// assemble rows one after the other
$rows_html .= " \n";
$count++;
} // endwhile cunhot query
// show table
$output_string.= "$uid $sortid $hotel_name $hotel_description $hotel_location \n";
$output_string.= $rows_html;
$output_string.= "
\n";
*/
########################################################
# FUNCTIONS, CALL VALUES, GLOBALS AND RETURN OF MULTIPLE VALUES
########################################################
$output_string.= "\n
\n";
// We have used for some time the build in functions
// (for while if isset mysql_query($query) etc...)
// We can build our own ones too:
// Functions break long scripts up in smaller blocks that can be re-used.
// the beauty is that all variables used inside the function stay there.
// Repeatedly used routines like date selectors, mysql queries, have a look
// at the library.PHP file accomanying this COURSE
function funct_name($var1, $var2,$varx){
// do something with the input vars
return result;
}
// The bad thing is that we can not return more then one value. But don't
// despair, there are two methods around that problem:
// 1.- declare the other value you need to return as a global.
// not a valid idea in 2013, there are no more globals
// As soon as you called the function, the value will be available
// outside the function.
$var1 = "A";
$var2 = "B";
$varx = "C";
function thename($var1, $var2,$varx){
global $result2;
// do something with the input vars
$result = "OK
";
$result2 = "OPERATING
";
return $result;
}
//call function
$output_string.= thename($var1,$var2,$varx);
$output_string.= $result2;
// 2.- return an array
function thename2($var1, $var2,$varx){
// do something with the input vars
$status["result1"] = "OK";
$status["result2"] = "OPERATING";
$status["result3"] = "HIGH";
return $status;
}
//call function
$array = thename2($var1,$var2,$varx);
// DISPLAY THE ELEMENTS
$output_string.= $array["result1"] . "
";
$output_string.= $array["result2"] . "
";
$output_string.= $array["result3"] . "
";
########################################################
######################## DATES TIME DATE STRTOTIME JULIANDATECOUNT
########################################################
$output_string.= "\n
\n";
// FACE IT: sooner or later you will have to deal with dates
// The computer calculates everything with timestamps SECONDS
// SINCE UNIX EPOCH 1970-01-01
// get used to it !!!
// CREATE TIMESTAMPS
// witout formatting, use default
$nowdate= time();
// with custom formattiong
$nowdate=gmdate("D, d M Y H:i:s");
$output_string.= "
";
//here are my favorite DATE snippets
// format a date to whatever you want
// Make stamp from date
$UseDateS=strtotime($event_start);
// or NOW
$UseDateS=time();
// add three days
$UseDateE=$UseDateS+(3* 86400);
// date accepts a second parameter, you guessed it, a timestamp
// which in turn gets formatted like the first variable's template shows
$vacation_start=date("Y-m-d",$UseDateS);
// get difference in days
$datediff=(($UseDateE-$UseDateS)/86400);
$output_string.= $datediff . "
";
// see THAT number?
// !!!! 86.400 that is the number of seconds of a day!!!
// add 30 days to a timestamp
$output_string.= "before and thirty days later
";
$output_string.= $UseDateS . "
";
$UseDateS=time()+(30*86400);
$output_string.= $UseDateS . "
";
function date_to_days($date){
$datetime = strtotime($date);
$datearr["year"] = date("Y",$datetime);
$datearr["month"] = date("m",$datetime);
$datearr["day"] = date("d",$datetime);
return $datearr;
}
function days_to_date($year,$month,$day){
// LEAVING FOR THIS TIME SECONDS MINUTES AND HOURS IN ZERO
$UseDateS=mktime(0, 0, 0, "$month", "$day", "$year");
$datez=date("Y-m-d",$UseDateS);
return $datez;
}
function add_days_to_date($here_and_now, $days_per_stay){
$newdate_arr = date_to_days($here_and_now);
$days_added = $newdate_arr[day] + $days_per_stay;
$newdate = days_to_date($newdate_arr[year],$newdate_arr[month],$days_added);
return $newdate;
}
$output_string.= "Now and five days later
";
$output_string.= $vacation_start . "
";
$output_string.= add_days_to_date($vacation_start, 5) . "
\n";
// add_days_to_date is a function I wrote, it does not come with php.
// If you need any date before 1900 you need to use the julian day count.
// that thing goes back over 3000 years
$juliandaycount = 1876000;
$output_string.= "Julian daycount:" . $juliandaycount . "
";
$output_string.="
" . jdtogregorian($juliandaycount) . "
";
$juliandaycount = 1;
$output_string.="
" . jdtogregorian($juliandaycount) . "
";
$output_string.="That is 25 November 4714 BC!
";
// go from there...
// lets try if strtotime can handle that date...
$output_string.="
" . date("Y M d ",strtotime(jdtogregorian($juliandaycount))) . "
";
// there you have it: date does not work before 1970
// dates in different languages
// change to spanish dates
setlocale(LC_ALL, 'es_ES');
/* Output: Viernes 22 diciembre 1978 */
// echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));
// Now date in spanish
$output_string.= strftime("%A %e %B %Y") . "
\n";
// with a specific time stamp
$output_string.= strftime("%A %e %B %Y", strtotime($vacation_start)) . "
\n";
########################################################
############################################## MATH FUNCTIONS
########################################################
// Math functions can calculate totally exact or do logical comparisons
$output_string.= "\n
\n";
// fmod: the modulo of a number
$column_count = 3;
$counter = 4;
$output_string.= "$counter $column_count
";
$output_string.= fmod ($counter,$column_count) . "
";
// inside a loop you might find this code fragment
// to do something every X times out of a totalcount
If (fmod ("$counter",$column_count) == 1){
$output_string.= "add table commands
";
$cnt = $cnt +$column_count;
}
// the math therm is modulo
$divisor = $column_count;
$variable = $counter;
$rest = fmod($variable, $divisor);
$output_string.= "Modulo (4/3) = " . $rest . "
";
// bcmod(5, 4) evaluates to 1 (the rest)
// another very useful function is
// syntax
// round ($number, $digits);
$value = "23.87654563";
$output_string.= " Round $value to two digits after the comma: ";
$output_string.= round($value,2) . "
";
$output_string.= " Round $value to NO digits after the comma: ";
$output_string.= round($value) . "
";
// there are lots of functions in the math section like
// sin, cos, tan, pi, etc...
########################################################
############################################ FUNCTION LIBRARY
########################################################
$output_string.= "\n
\n";
// the library.PHP is a php file with all kinds of code snippets.
// THAT is YOUR CONSTRUCTOR KIT!!!
// copy / paste your programms together...
// DON'T BE SHY...
########################################################
##################################### BROWSE FUNCTIONS AT PHP.NET!
########################################################
$output_string.= "\n
\n";
// if you have a rough idea what you are looking for, php.net is
// the REFERENCE!
// the online function reference is very well documented
// and has lots of examples
########################################################
################## PROGRAMMING STRUCTURE
\n";
// to make your programming life easier, please memorize this four categories:
//
";
$memorize_this.= "VARIABLES, FUNCTIONS, CONDITIONS AND HTML !!!
";
$output_string.= $memorize_this;
// 1.
\n";
// THANK GOD FOR THE INCLUDES!!!
// includes give you a way to keep a long program in several files/modules
// you can add an include anywhere inside the script.
// include("coretop.php");
// include("/home/yourdomain.com.mx/htdocs/core/user/$associ/$associ_lang.inc.php");
// include_once("coretop.php");
// include_once("http://bpf3.local/~bernhard/yourdomain.com/htdocs/coretop.php");
// if include fails to include the file, script continues loading
// the rest of the code
# ---------------------------------------------------------------
//!!! disabled require("coretop.php");
//!!! disabled require_once("coretop.php");
// if require fails to include file, script stops.
// !!! The "_once" means if it was already included,
// it will not be included a second time.
// here is a good example of includes, the index file of THESITE,
// a fast prototype builder we use internally
# ---------------------------------------------------------------
/* !!!!!!!!! DISABLED... IT JUST GOT TOO MESSY
$site_directory = "/webservices/thesite";
include($_SERVER["DOCUMENT_ROOT"] . $site_directory . "/site_header.php");
include("cat_header.php");
include("$site_include_directory/site_js.php");
echo " \n";
echo "\n";
include("$site_include_directory/site_nav.php");
include("cat_nav.php");
echo " \n";
echo "\n";
include("cat_content.php");
echo " \n";
include("$site_include_directory/site_footer_nav.php");
include("$site_include_directory/site_footer.php");
include("$site_include_directory/site_design_end.php");
echo "\n";
echo "\n";
# ---------------------------------------------------------------
END DISABLED !!!!!!! */
// very useful for a structured approach to load design and
// other page elements.
// thats it for includes...
// headers are another golden command! You can redirect to
// a new page inside conditions, depending on the condition.
// header("Location:http://www.cancun.net");
// header("Location:/home/yourdomain.com/htdocs/yourdirectory/yourfile.php?force=ABC");
$output_string.= "can not show this life or you end up in cancun online.\n";
########################################################
################################################### PARSE_INI
########################################################
$output_string.= "\n
\n";
// a call to this function will INSERT all values specified in the file
// into the executing script. This ini file has a laxer syntax then php
// and is required to be a textfile.
// examples of the php.ini file:
// variable_name = 45
// variable_name2 = 'text value'
// array[name] = '$value1'
$output_string.= "A one liner function call ";
parse_ini_file("/home/etc/php.ini");
$output_string.= "and Voila, the default settings are loaded.
";
$output_string.= "CHECK mysql.allow_persistent: " . $mysql.allow_persistent . "
";
########################################################
######################################### CALI AUTHENTICATION
########################################################
$output_string.= "\n
\n";
// if you ever need to protect a script with a password, use PHPLib.
/* Again disabled because the includes load km of code
include("/home/yourdomain.com.mx/php/prepend.php3");
page_open(array("sess" => "Example_Session", "auth" => "Example_Challenge_Crypt_Auth", "perm" => "Example_Perm", "user" => "Example_User"));
$auth->login_if($again); // relogin, if this was requested...
if ($perm->have_perm("admin") OR $perm->have_perm("supervisor") OR $perm->have_perm("editor")) {
// do whatever the condition tells you
}
page_close();
end disabled */
########################################################
######################################## FILE AND DIRECTORIES
########################################################
$output_string.= "\n
\n";
function getDirectoryTree( $outerDir , $x){
$dirlist = "";
$dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) );
$dir_array = Array();
foreach( $dirs as $d ){
if( is_dir($outerDir."/".$d) ){
$dir_array[ "$d" ] = getDirectoryTree( $outerDir."/".$d , $x);
$dirlist.= $d . "
";
}else{
for($cnt=0;$cnt
";
}
}
$text = "You know what?????\n";
$text.= "
\n";
$text.= "
R T F M -- Read The Fucking Manual, Man... r.t.f.m!\n";
$output_string.= "$text
";
// without the dot, $text starts here.
$text = "Go to PHP.NET and ask for the chapter on the FILE function\n";
$text = "Enjoy the next years programming with PHP!!!\n";
$text = "Saludos from the Tropical Cancun\n";
$output_string.= "$text
";
##########################################
# I am finished.... You know already more then me! #
##########################################
// cut -> paste -> in new document
// modify -> upload -> reload page -> modify -> upload -> reload ...
// try try try
// /* WITH COMPASSION */
// ` /* 2006-25-FEB */
// /* Bernhard */
# another comment
$output_string.= "";
// if you did not figure it out by yourself, click on the
// phpcrashcourse title to see the results.
?>
";
echo show_source(__FILE__);
}
echo "© " . date("Y") . " " . $_SERVER["PHP_SELF"];
// sorry for the php mess in the html section, but the show_source
// will output immediately and can not be reassined assigned to our
// $output_string
?>