Difference between revisions of "Php"
(→PHP and GraphViz) |
(→PHP and GraphViz) |
||
Line 44: | Line 44: | ||
</svg> | </svg> | ||
</html> | </html> | ||
+ | |||
+ | This of course opens a world of opportunities, like mindmap extensions in wiki: https://www.mediawiki.org/wiki/Extension:QuickGV | ||
= Debugging = | = Debugging = |
Latest revision as of 16:19, 28 May 2016
Contents |
PHP and GraphViz
Just learned how to use GraphViz (command dot) on a website using PHP. First you must have graphviz installed on your Linux webserver. Then use this code:
<?php $streams = array ( 0=>array("pipe","r"), 1=>array("pipe","w") ); $handle = proc_open('dot -Tsvg',$streams,$pipe); fwrite($pipe[0], 'digraph "Wikimap" {"PHP" -> "GraphViz" }'); fclose($pipe[0]); echo stream_get_contents($pipe[1]); fclose($pipe[1]); proc_close($handle); ?>
And the output looks like this:
This of course opens a world of opportunities, like mindmap extensions in wiki: https://www.mediawiki.org/wiki/Extension:QuickGV
Debugging
It happens often that I see an empty page where I would expect something. One example is when I migrate a server and have a php script running on a new server. It may turn out that this new server does not have a specific PHP module and that is why it does not work. Similarly, if I forget a ";" at the end of a command, the whole php file will just show nothing in the browser. To troubleshoot this, change your php.ini file, e.g. /etc/php5/apache2/php.ini and make sure you have a line like
error_log = php_errors.log
Then you will find the php_errors.log file in the respective directory. For example if there is a problem with /var/www/staerk.de/dokuwiki/doku.php there will be a file /var/www/staerk.de/dokuwiki/php_errors.log telling you the errors as you are used to from command line:
[12-Dec-2015 15:34:16 UTC] PHP Parse error: syntax error, unexpected '$NS' (T_VARIABLE), expecting ',' or ';' in /var/www/staerk.de/dokuwiki/doku.php on line 34
output
Here is a php trick that outputs debugging output when you want.
$handler=fopen("/tmp/debug","a"); fwrite ($handler, "hello world"); fclose ($handler);
example for variables:
$handler=fopen("/tmp/debug","a"); fwrite ($handler, var_export($variable,true)); fclose ($handler);
variables' content
To output an array use
echo serialize($array)
magic words
Parameters
$argv[1]