Difference between revisions of "Php"

From ThorstensHome
Jump to: navigation, search
(Debugging)
Line 1: Line 1:
 +
= PHP and GraphViz =
 +
Just learned how to use GraphViz (command dot) on a website using PHP. Here is the code:
 +
<pre>
 +
<?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);
 +
?>
 +
</pre>
 +
 +
And the output looks like this:
 +
<html>
 +
<svg width="100pt" height="116pt"
 +
viewBox="0.00 0.00 100.29 116.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 +
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 112)">
 +
<title>Wikimap</title>
 +
<polygon fill="white" stroke="none" points="-4,4 -4,-112 96.2918,-112 96.2918,4 -4,4"/>
 +
<!-- PHP -->
 +
<g id="node1" class="node"><title>PHP</title>
 +
<ellipse fill="none" stroke="black" cx="46.1459" cy="-90" rx="28.6953" ry="18"/>
 +
<text text-anchor="middle" x="46.1459" y="-86.3" font-family="Times,serif" font-size="14.00">PHP</text>
 +
</g>
 +
<!-- GraphViz -->
 +
<g id="node2" class="node"><title>GraphViz</title>
 +
<ellipse fill="none" stroke="black" cx="46.1459" cy="-18" rx="46.2923" ry="18"/>
 +
<text text-anchor="middle" x="46.1459" y="-14.3" font-family="Times,serif" font-size="14.00">GraphViz</text>
 +
</g>
 +
<!-- PHP&#45;&gt;GraphViz -->
 +
<g id="edge1" class="edge"><title>PHP&#45;&gt;GraphViz</title>
 +
<path fill="none" stroke="black" d="M46.1459,-71.6966C46.1459,-63.9827 46.1459,-54.7125 46.1459,-46.1124"/>
 +
<polygon fill="black" stroke="black" points="49.646,-46.1043 46.1459,-36.1043 42.646,-46.1044 49.646,-46.1043"/>
 +
</g>
 +
</g>
 +
</svg>
 +
</html>
 +
 
= Debugging =
 
= Debugging =
 
It happens often that I see an empty page where I would expect something. One example is when I migrate a server  
 
It happens often that I see an empty page where I would expect something. One example is when I migrate a server  

Revision as of 15:17, 28 May 2016

Contents

PHP and GraphViz

Just learned how to use GraphViz (command dot) on a website using PHP. Here is the 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: Wikimap PHP PHP GraphViz GraphViz PHP->GraphViz

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

var_dump

To output an array use

echo serialize($array)

magic words

Parameters

$argv[1]