Advanced PHP Cheat Sheet
Tag it:
?
A cheat sheet containing Advanced Operators, Database MySQL, File Reading / Writing, File / Directory Handling, Function Handling, Object Functions, Constants and Magic Constants.
Advanced Operators
Instanceof -
instanceof
@ - @
() // Suppress generated errors/notices
Database MySQL
mysql_connect([
],[
],[
]); mysql_create_db(
,[
]); mysql_select_db(
,[
]); mysql_drop_db(
,[
]); mysql_list_dbs([
]); mysql_list_tables(
,[
]); mysql_list_fields(
,
,[
]); mysql_query(
,[
]); mysql_fetch_array(
,
); mysql_fetch_assoc(
); mysql_fetch_object(
,[
],[
]); mysql_fetch_row(
); // Example using fetch_array while ($result_row = mysql_fetch_array($result,MYSQL_ASSOC)) { echo "Field 1 is: {$result_row['field1']}"; } // Example using fetch_object while ($result_obj = mysql_fetch_object($result)) { echo "Field 1 is: " . $result_obj->field1; } mysql_affected_rows([
]); mysql_num_rows(
); mysql_num_fields(
); mysql_real_escape_string(
,[
]); mysql_close([
]);
File Reading / Writing
fopen(
,
,[
],[
]); fread(
,
); fwrite(
,
,[
]); fseek(
,
,[
]); fclose(
); is_readable(
); is_writeable(
); // Example of writing to file $file = fopen("hello.txt","w"); if ($file) { fwrite($file,"This is a line of text"); fclose($file); } // Example of reading from file $fileread = fopen("hello.txt","r"); if ($fileread) { $text = fread($fileread,100); fclose($fileread); echo "Read in: $text"; }
File/directory Handling
file_exists(
); is_file(
); dirname(
); is_dir(
); mkdir(
,[
],[
],[
]); rmdir(
,[
]); rename(
,
,[
]); copy(
,
,[
]); chmod(
,
); unlink(
]); // Example of using is_file, copy, and unlink to rename file. if (is_file("hello.txt")) { copy("hello.txt","goodbye.txt"); unlink("hello.txt"); } // Example of file_exists and rename if (file_exists("goodbye.txt")) { rename("goodbye.txt","helloagain.txt"); }
Function Handling
create_function(
,
); call_user_func(
[,
[,
[, ... ]]]); call_user_func(array(
,
)[,
[,
[, ... ]]]); call_user_func_array(
,
); function_exists(
); // Example of creating anonymous function (returns the value 6) $myNewFunction = create_function('$param1,$param2','return $param1 * $param2;'); echo "Result is: " . $myNewFunction(2,3); // Example of calling class method using parameter class MyClass { function showMessage($test = "") { echo "Welcome to DIC! $test"; } } call_user_func(array('MyClass','showMessage'),"Enjoy your stay.");
Object Functions
$variable = get_class([
]); $arrayofclassnames = get_declared_classes(); $variable = get_parent_class([
]); $arrayofproperties = get_object_vars([
]); is_a(
,
); is_subclass_of(
,
); class_exists(
); method_exists(
,
);
Constants
// Define a constant define(
,
,[
]);
Magic Constants
__LINE__ - Current line number of the file. __FILE__ - The full path and filename of the file. If used inside an include, the name of the included file is returned. __FUNCTION__ - The function name. __CLASS__ - The class name. __METHOD__ - The class method name. (PHP 5.0+)
Comments
Search
Only registered users can write comments!
Last Updated ( Sunday, 22 June 2008 )