PHP Editor:
<?php print "Hello Web!"; ?>
<?php phpinfo(); ?>
php helloworld.php
c:\Program Files (x86)\PHP\php.exe helloworld.php
check http://192.168.2.11/info.php(To run with debug mode, remove option –disable-debug when build PHP from source). In php.ini, we need turn on log_errors = On for production and development and where to save log file
error_log = /var/log/php_errors.log
To be can save log to php_errors.log, you must change own to allow http write log:
chown www.www /var/log/php_errors.log
Config turn on/off debug log in php:
error_reporting = E_ALL & ~E_DEPRECATED display_errors = Off display_startup_errors = Off track_errors = Off
error_reporting = E_ALL display_errors = On display_startup_errors = On track_errors = On
Config turn on debug log in ngix with php-fpm backend:
[global] error_log = /var/log/php-fpm.log ;log_level = notice [www] catch_workers_output = yes
display_errors = On ⇒ Allow display debug information in front end
Config turn on/off log in httpd:
php_flag display_errors on php_value error_reporting 2039
error_reporting(E_ALL & ~E_NOTICE); if (!ini_get('display_errors')) { ini_set('display_errors', '1'); }
ErrorLog "logs/error_log"
LogLevel error
###demo.vn server { listen 80; server_name demo.vn www.demo.vn; root /data/www/gamebai/demo; index index.html index.htm index.php; fastcgi_index index.php; access_log /gb/nginx/logs/demo.access_log; error_log /gb/nginx/logs/demo.error_log; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location ~* "^.+\.(js|ico|gif|jpg|png|css|swf|htc|xml|bmp)$" { access_log off; expires 30d; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
⇒ error log: error_log /gb/nginx/logs/demo.error_log;
<VirtualHost *:80> DocumentRoot "D:/web/gamebai/demo" ServerName demo.vn DirectoryIndex index.php index.html index.htm <Directory D:/web/gamebai/demo> Options Indexes FollowSymLinks MultiViews AllowOverride all Order Deny,Allow Allow from all </Directory> ErrorLog "logs/demo-error.log" CustomLog "logs/demo-access.log" common </VirtualHost>
⇒ error log: ErrorLog “logs/demo-error.log”
print(variable_name)
echo "<pre>";var_dump(debug_backtrace());echo "</pre>";
⇒ Print name of class for checking their functions
try{ } catch(Exception $e){ echo $e->getMessage(); } ?>
Refer: Debug PHP with xdebug
wget -O phpunit https://phar.phpunit.de/phpunit-5.phar chmod +x phpunit ./phpunit --version
composer require --dev phpunit/phpunit ^5 ./vendor/bin/phpunit --version
request_slowlog_timeout = 5s slowlog = /var/log/php-fpm/slowlog-site.log
<?php ?>
<? ?>
<% %>
<SCRIPT LANGUAGE="php"> </SCRIPT>
The standard and the script tags are guaranteed to work under any configuration, the other tags be enabled in your “php.ini”
//your commment #your commment /*your commment */
<html> <title>HTML with PHP</title> <body> <h1>My Example</h1> <?php //your php code here ?> <b>Here is some more HTML</b> <?php //more php code ?> </body> </html>
<?php Echo "<html>"; Echo "<title>HTML with PHP</title>"; Echo "<b>My Example</b>"; //your php code here Print "<i>Print works too!</i>"; ?>
if (! isset($cars)) { $cars = $default_cars; } $defaults = array('emperors' => array('Rudolf II','Caligula'), 'vegetable' => 'celery', 'acres' => 15);
If you want a local variable to retain its value between invocations of a function, Declare the variable as static:
function track_times_called( ) { static $i = 0; $i++; return $i; }
function eat_fruit($fruit) { global $chew_count; for ($i = $chew_count; $i > 0; $i--) { ... } }
function eat_fruit($fruit) { for ($i = $GLOBALS['chew_count']; $i > 0; $i--) { ... } }
__LINE__ The current line number of the file. __FILE__ The full path and filename of the file. __DIR__ The directory of the file. __FUNCTION__ The function name. __CLASS__ The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. __METHOD__ The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive). __NAMESPACE__ The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0). DIRECTORY_SEPARATOR "..".DIRECTORY_SEPARATOR."foo" => windows: "..\foo" and linux: "../foo"
function add($a, $b) { return $a + $b; } $total = add(2, 2);
function wrap_html_tag($string, $tag = 'b') { $string = "<$tag>$string</$tag>"; return $string; } $string = 'I am some HTML'; wrap_html_tag($string); print "$string\n"; $string = wrap_html_tag($string); print "$string\n";
Out put:
I am some HTML <b>I am some HTML</b>
function wrap_html_tag(&$string, $tag = 'b') { $string = "<$tag>$string</$tag>"; return $string; } $string = 'I am some HTML'; wrap_html_tag($string); echo $string;
Out put:
<b>I am some HTML</b>
require and include. The main difference is that attempting to require a nonexistent file is a fatal error, while attempting to include such a file produces a warning but does not stop script execution.
<? include 'header.html'; ?> content <? include 'footer.html'; ?> <? require 'codelib.inc'; ?> mysub( );// defined in codelib.inc
In order to store the contents of a shopping cart while the user is browsing your site, There are two easy ways: you either use cookies or sessions.
print $HTTP_COOKIE; //outputs "visits=23" print getenv("HTTP_COOKIER"); //outputs "visits=23" print $visits; //outputs "23" print $HTTP_COOKIE_VARS[visits]; //outputs "23"
//don't output anything before this header("visits=23; expires=Friday, 20-Aug-04 03:27:21 GMT; path=/; domain=softwareprojects.org"); setcookie("hits", 23, time() + 3600, "/","softwareprojects.org", 0); //notice this last extra argument
session_start(); if(isset($stored_var))//if(session_is_registered("stored_var")) { print $stored_var; //this will not be displayed the first time you load the page, because you haven't registered the variable yet! } else { $stored_var = "Hello from a stored variable!"; session_register("stored_var"); //don't do this: session_register($session_var) }
session_start(); session_register("test"); $test = 12; session_unset(); //$test is destroyed session_destroy(); print $test; //outputs nothing
if(!mysql_connect("localhost", "bob", "secret")) //tries to connect to the database server die("Unable to connect to the database server!"); //terminates the script, and outputs the error
'connect' => 'mysql_connect', 'pconnect' => 'mysql_pconnect', 'select_db' => 'mysql_select_db', 'query' => 'mysql_query', 'query_unbuffered' => 'mysql_unbuffered_query', 'fetch_row' => 'mysql_fetch_row', 'fetch_array' => 'mysql_fetch_array', 'fetch_field' => 'mysql_fetch_field', 'free_result' => 'mysql_free_result', 'data_seek' => 'mysql_data_seek', 'error' => 'mysql_error', 'errno' => 'mysql_errno', 'affected_rows' => 'mysql_affected_rows', 'num_rows' => 'mysql_num_rows', 'num_fields' => 'mysql_num_fields', 'field_name' => 'mysql_field_name', 'insert_id' => 'mysql_insert_id', 'escape_string' => 'mysql_escape_string', 'real_escape_string' => 'mysql_real_escape_string', 'close' => 'mysql_close', 'client_encoding' => 'mysql_client_encoding', 'ping' => 'mysql_ping',
'connect' => 'mysqli_real_connect', 'pconnect' => 'mysqli_real_connect', // mysqli doesn't support persistent connections THANK YOU! 'select_db' => 'mysqli_select_db', 'query' => 'mysqli_query', 'query_unbuffered' => 'mysqli_unbuffered_query', 'fetch_row' => 'mysqli_fetch_row', 'fetch_array' => 'mysqli_fetch_array', 'fetch_field' => 'mysqli_fetch_field', 'free_result' => 'mysqli_free_result', 'data_seek' => 'mysqli_data_seek', 'error' => 'mysqli_error', 'errno' => 'mysqli_errno', 'affected_rows' => 'mysqli_affected_rows', 'num_rows' => 'mysqli_num_rows', 'num_fields' => 'mysqli_num_fields', 'field_name' => 'mysqli_field_tell', 'insert_id' => 'mysqli_insert_id', 'escape_string' => 'mysqli_real_escape_string', 'real_escape_string' => 'mysqli_real_escape_string', 'close' => 'mysqli_close', 'client_encoding' => 'mysqli_client_encoding', 'ping' => 'mysqli_ping',
Using tool: https://github.com/philip/MySQLConverterTool
php MySQLConverterTool-master/cli.php -f changeusername.php > changeusername_new.php
And Modify some missing in tool:
mysqli_connect("127.0.0.1", "forum_ttb", "xxxxxxxxxx"));
to
=> mysqli_connect("127.0.0.1", "forum_ttb", "xxxxxxxxxx","forum_ttb"));
<?php function curl_post($url, $vars=array(), $second=10) { try{ $ch = curl_init(); if ($ch === false) { throw new Exception('failed to initialize'); } curl_setopt($ch,CURLOPT_TIMEOUT,$second); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,$vars); $data = curl_exec($ch); if ($data === false) { throw new Exception(curl_error($ch), curl_errno($ch)); } curl_close($ch); }catch(Exception $e){ echo "Curl Error: " . $e->getCode . $e->getMessage(); } if($data) return $data; else return false; }