PHP File Write Debugging Primer Tutorial

PHP File Write Debugging Primer Tutorial

PHP File Write Debugging Primer Tutorial

Looking at PHPStorm and Xdebug Primer Tutorial below reminds us that PHP can be debugged in that “step through the code” way that is sometimes useful. The work of yesterday’s Australian Postcode Place Images State Precedence Tutorial caused us to think about PHP debugging regarding a scenario …

  • that occurred sporadically (ie. data based issue, or at least our interfacing to that data (“a bad carpenter blames his tools”)) … and presented in one of the most cryptic ways possible (“a programmer’s nightmare”)) …
  • symptom, on these sporadic occasions and running in Safari web browser, its (Develop -> Show Web Inspector) Web Inspector “Console” tab shows (a last) error …

    SyntaxError: Unexpected token ';'           fgc:1

    … before not displaying the Wikipedia images

Optimistic programmers will be thrown by … anyone, anyone? Yes, Gottfried, the “1” is supremely disappointing. A “real” codeline “1” is very unlikely, though can happen as a result of “eval” in Javascript code. (Spoiler alert: can happen in a continuous line of Javascript written out in that fashion by PHP). In our defence, even if we had immediately tweaked to “can happen in a continuous line of Javascript written out in that fashion by PHP” that would not have helped enormously.

But what did we know? We saw a pattern of this error regarding our “fgc” directory index.php PHP calling …

<?php

function relative_to_absolute($inth, $firstonly) {
// lots of code follows
}

?>

… but it resulted in …

  • no Wikipedia images shown but no malfunctioning of the resultant webpage, otherwise … and …
  • calling PHP debugging could not determine for us whether the problem occurred …
    1. within relative_to_absolute function … or …
    2. as a result of the return values as a result of executing relative_to_absolute function

And here’s the thing for today’s relevance. We decided not to involve any third party tools to debug the PHP. We decided to use a “PHP File Write Debugging Approach” a little bit like Javascript debugging can be achieved by “console.log” (or maybe “alert”) messages directed to the user.

Where did we intervene? In and around “fgc” PHP calls of “relative_to_absolute”.

And how did we intervene? Well, here, for the first time we can recall, we wrote a (temporarily used) PHP “debugging” function of the (wrapper to file_put_contents) fashion …

<?php

$dcount=0;

function debugfile_put_contents($infn, $inct) {
global $dcount;
// if you want to leave this function in later after debugging, along with its calling codelines, make this codeline be "return;"
if ($inct == "") {
$dcount=0;
while (file_exists('' . $dcount . '_junk.txt')) {
unlink('' . $dcount . '_junk.txt');
$dcount++;
}
$dcount=0;
file_put_contents('' . $dcount . '_junk.txt', "");
} else {
file_put_contents($infn, $inct);
file_put_contents('' . $dcount . '_junk.txt', $infn . ': ' . $inct);
$dcount++;
}
}

?>

… where up the top of “relative_to_absolute” we’d place

<?php

function relative_to_absolute($inth, $firstonly) {
global $udirname, $latis, $longis, $countryname, $countrycode, $bp, $pbbm, $wikiall, $oneis, $ithree;
debugfile_put_contents("junJUNKket7.txt", "");
// lots more code follows
}

?>

… to “clean the slate”, so to speak, for each call of “relative_to_absolute”, and then at “interest points” codelines like

<?php

$bp="";

function relative_to_absolute($inth, $firstonly) {
global $udirname, $latis, $longis, $countryname, $countrycode, $bp, $pbbm, $wikiall, $oneis, $ithree;
debugfile_put_contents("junJUNKket7.txt", "");
// lots more code follows ... then at that "finally arrived at "interest point" that happened to be at the end of the code of this function, it pans out ...
debugfile_put_contents("junket6p.txt", $outth);
if (!$firstonly) {
debugfile_put_contents("junket6pa.txt", $outth);
if ($bp != "") {
debugfile_put_contents("junket6pb.txt", $outth);
$bp.="\";";
debugfile_put_contents("junket6pc.txt", $bp);
} else {
debugfile_put_contents("junket6pd.txt", $outth);
$bp=" parent.document.getElementById(\"ourcanvas\").setAttribute(\"data-geo\",\"\"); parent.document.getElementById(\"ourcanvas\").style.background=\"\";";
debugfile_put_contents("junket6pe.txt", $outth);
}
debugfile_put_contents("junket6pf.txt", $outth);
$bp=str_replace('";,', ',', $bp);
return $outh;
}
debugfile_put_contents("junket6q.txt", $outth);
}
} catch (Exception $wuy) { }
debugfile_put_contents("junket6r.txt", $outth);
if ($wikiall) { return str_replace("https://", "//", str_replace("//", "//", str_replace("debugfile_put_contents("junket6s.txt", $outth);
return $outth;
}

?>

… the debugging results getting us to add in the “winning new codeline”, as shown above, to solve our dilemma, given our “findings reporting regime” shown (via macOS Terminal) below …


user@Users-MacBook-Air htdocs % ls -lct PHP/fgc/*.* | more
-rw-r--r-- 1 user admin 96697 18 Oct 18:25 PHP/fgc/99_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6pf.txt
-rw-r--r-- 1 user admin 321 18 Oct 18:25 PHP/fgc/98_junk.txt
-rw-r--r-- 1 user admin 306 18 Oct 18:25 PHP/fgc/junket6pc.txt
-rw-r--r-- 1 user admin 96697 18 Oct 18:25 PHP/fgc/97_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6pb.txt
-rw-r--r-- 1 user admin 96697 18 Oct 18:25 PHP/fgc/96_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6pa.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/95_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6p.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/94_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6f.txt
-rw-r--r-- 1 user admin 96697 18 Oct 18:25 PHP/fgc/93_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6ff.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/92_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6d.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/91_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6c.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/90_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6b.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/89_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6a.txt
-rw-r--r-- 1 user admin 96695 18 Oct 18:25 PHP/fgc/88_junk.txt
-rw-r--r-- 1 user admin 96682 18 Oct 18:25 PHP/fgc/junket6.txt
-rw-r--r-- 1 user admin 66 18 Oct 18:25 PHP/fgc/87_junk.txt
-rw-r--r-- 1 user admin 52 18 Oct 18:25 PHP/fgc/junket2b.txt
-rw-r--r-- 1 user admin 66 18 Oct 18:25 PHP/fgc/86_junk.txt
-rw-r--r-- 1 user admin 52 18 Oct 18:25 PHP/fgc/junket2a.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/85_junk.txt
-rw-r--r-- 1 user admin 96697 18 Oct 18:25 PHP/fgc/84_junk.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/83_junk.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/82_junk.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/81_junk.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/80_junk.txt
-rw-r--r-- 1 user admin 96695 18 Oct 18:25 PHP/fgc/79_junk.txt
-rw-r--r-- 1 user admin 66 18 Oct 18:25 PHP/fgc/78_junk.txt
-rw-r--r-- 1 user admin 66 18 Oct 18:25 PHP/fgc/77_junk.txt
-rw-r--r-- 1 user admin 66 18 Oct 18:25 PHP/fgc/76_junk.txt
-rw-r--r-- 1 user admin 96696 18 Oct 18:25 PHP/fgc/75_junk.txt
user@Users-MacBook-Air htdocs % cat PHP/fgc/98_junk.txt
junket6pc.txt: parent.document.getElementById("ourcanvas").setAttribute("data-geo",""); parent.document.getElementById("ourcanvas").style.backgroundPosition="left top,center top,right top,right center";,left top,center top,right top,right center,right bottom,center bottom,left bottom,left center,left top,center top";% user@Users-MacBook-Air htdocs % head -5 PHP/fgc/99_junk.txt
junket6pf.txt: <!DOCTYPE html>
<html class="client-nojs" lang="en" dir="ltr">
<head>
<meta charset="UTF-8"/>
<title>Lawson, New South Wales - Wikipedia</title>

… the gadzooks finding, above, tweaking with us that the PHP, with certain incoming data, could fail with its writing out of the Javascript DOM backgroundPosition code. Aaaaahhh, that all figures!

Cute, huh?!


Previous relevant PHPStorm and Xdebug Primer Tutorial is shown below.

PHPStorm and Xdebug Primer Tutorial

PHPStorm and Xdebug Primer Tutorial

Though with a large variety of scenarios with code, you can debug a piece of code from the command line … and don’t get me wrong because sometimes the command line will be the only way … the experience of debugging is much better if it is combined with work within an IDE.

In this scenario, what about debugging PHP? We’ve been trying out PHPStorm, we talked about at PHPStorm IDE Primer Tutorial, as shown below, overseeing PHP projects and using the Xdebug debugger.

Roughly speaking, what is required here is, via the Xdebug Configuration advice from JetBrains, the inventors of PHPStorm …

  • If Xdebug is not installed, an Xdebug install would be required … and then …
  • PHPStorm Preferences -> Languages and Frameworks -> PHP -> “…” Button gives you both …
    1. Location of relevant php.ini (which you’ll probably change) … and …
    2. Xdebug (or other debugger) existance status (to tell you if you are getting anywhere)
  • Changes to that php.ini (of above) with, in our case, lots of stops and starts of our local MAMP Apache web server (to see those changes right)

Cutting to the chase what we found could work with our php.ini (after dismantling any pre-existing [zend] section blockages (though we didn’t have this)) was …


[xdebug]
zend_extension="/Applications/MAMP/bin/php/php5.4.10/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so"
;zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_port=9123
xdebug.remote_host="127.0.0.1"
xdebug.remote_handler="dbgp"
xdebug.remote_mode="req"
xdebug.remote_connect_back=0
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/Library/pgAgent/Downloads/ansible-deployment/tmp"

… and we have to thank this link, in particular, among many from the Open Source community, which helped … thanks. Perhaps the most contentious configuration above is xdebug.remote_autostart=1 which can cause extreme slowness on occasions … if that is the way for you you may want to make it xdebug.remote_autostart=0 to not have the connection between Xdebug and PHP be checked so often. And the paths shown were specifically suited only here, we hasten to add … dough doh!

Other traps for young players could be …

  • Start Listening for PHP Debug Connections (Run option) is useful (rather than “Stop” … doh!)
  • Set some breakpoints
  • Break at the First Line of Code (Run option) quite useful
  • The initial connection between PHPStorm and Xdebug may take quite some time, and remember to accept the connection, should a connection window appear

Today’s slideshow offering is warts and all as life is when you cannot be sure what happens next?!


Previous relevant PHPStorm IDE Primer Tutorial is shown below.

PHPStorm IDE Primer Tutorial

PHPStorm IDE Primer Tutorial

Unlike compiled software code, PHP code is often coded simply with the use of a good text editor. That doesn’t mean programmers are not looking for a good PHP IDE if it comes along. Today we are going to talk, for the first time at this blog, about a great IDE to use with PHP based projects. It is called PHPStorm for good reason, as it is jam packed full of functionality and features, such as …

  1. PHP Code Editor
    • PHP 5.3, 5.4, 5.5, 5.6 & 7 support
    • smart code completion
    • syntax highlighting
    • extended code formatting configuration
    • on-the-fly error checking
    • code folding
    • language mixture support
  2. Debugging, Testing and Profiling
    • context-relevant local variables and user-defined watches
    • edit values on the fly
    • remote debugging for your server
    • debug in multiple sessions simultaneously
    • keep debugging session alive while moving between pages
  3. HTML/CSS/JavaScript Editor
    • DOM-based, browser-specific completion
    • live editing preview
    • utilize all the power of Emmet (formerly Zen Coding)
    • JavaScript refactorings available are Rename, Extract Variable/Function, Inline Variable/Function, Move/Copy, Safe Delete, Extract embedded script into file
    • automatic compilation/transpilation of modern web development languages to JavaScript (from CoffeeScript, TypeScript, Dart, etc) or CSS (from Sass, SCSS, Less, Stylus, Compass, etc.)
    • JavaScript debugger
    • auto-fixes for such problems as: missing required attributes, invalid attributes or illegal values, wrong references to files in links, duplicate attributes, invalid CSS selector format, invalid CSS properties, unused CSS class definitions, invalid local anchors
    • Angular 2, TSLint, JSLint/JSHint, Node.js, Flow support
  4. Development Environment
    • version control for Git, Subversion, Mercurial, Perforce, CVS, TFS supported
    • tools and code assistance features for work with database and SQL in your projects
    • analyze schema with UML diagrams
    • track any changes made to your source files, protecting you from any accidental losses or modifications
    • helps developers to understand and change their code by providing editable UML class diagrams for PHP code
    • plugin repository contains 250+ IDE plugins of all kinds
    • Vagrant is a handy tool, allowing you to share a development virtual machine, is natively integrated
    • REST Client is integrated in the IDE
    • SSH Console let you connect to any remote machine and perform various actions via SSH
    • Google App Engine applications for PHP development along with ability to delegate routine deployment tasks to the IDE
    • Composer (dependency manager for PHP) is supported
    • pre-configured command completion for various external command-line tools, including Composer, Symfony console, Zend Framework 2 tool (ZFTool), Zend Framework 1 tool, Drush for Drupal, Tools based on Symfony console (Laravel, Doctrine)
    • ability to use a remote interpreter instead of a local one with PHP based application on real server or a virtualized one created with Vagrant
    • completes and checks standard tags, properties, target names, path attribute values in build files, via Phing
    • same integrated development environment on Windows, Mac OS X and Linux with your single key
    • Docker plugin allows user to add Docker support to existing projects, view logs, manage Docker containers
    • if ToDo list is in an issue/bug tracking system, you can configure PhpStorm to work with the issues without switching from the IDE

… and more features can be read about from the PHPStorm website, by JetBrains. The information above comes from the website.

With today’s slideshow, we take up our introductory look at PHPStorm after the install (onto a MacBook Pro laptop using a local Apache MAMP web server) and download, opening some existant PHP code, as a PHPStorm project, which we then Run (or Execute).

We hope this is food for thought for you, in months not starting with M (because we want to remind you to “beware the ides of March” … boom, boom), using the great PHP language in a more integrated and dynamic environment, to hand coded, text edited PHP.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>