This fragment is about to be reported (you'll remain on this page):

You can enter a comment to clarify the mistake if you would like to:

Posts tagged “PHP” RSS20

The PHP dualism – self and static

Explains the difference between two keywords and probably the only case when self versus static still wins.

The PHP dualism – self and static

Version 5.2 introduced a (yet another) funny situation in PHP: static methods were possible and they could be overriden like in a proper OOP language but… well, they couldn’t be overriden «entirely».

The deal was that in 5.2 you couldn’t get class name of the object which static method is being called from within that method (it was possible for instance methods with PHPget_class($this);). PHP used self to refer to such object from within its own static method but it would always result in PHP__CLASS__ – that is, the name of class where this method implementation is defined. Basically, it was the same as copying class’ name from PHPclass Name { ... }.

Interestingly you couldn’t even use debug_backtrace as, naturally, PHP wouldn’t and couldn’t show you the proper name there.

But this, of course, was not satisfactory and programmers raged and demanded «real OOP» – even if only for static methods. Overall it looked like a quick fix to get 5.2 out.

«…»

No comments yet | Read rest of this entry »

When @ is actually dangerous

A memorandum on the error suppression in PHP and why you are better off avoiding it altogether.

When @ is actually dangerous

With PHP many things are funny once you hit them several times. In particular, one of the good-looking feature used (and even abused) by mostly newbie PHP devs is the error suppression operator – the «at» symbol (@).

At first it looks convenient making your code very concise:

The list can go on but the idea is clear: @ can be used to tell PHP not to cry out on undefined variables/indexes/errors because «you’ve got it under control».

The problem is that PHP will abide just too readily. Let’s see what unexpected consequences it may cause.

«…»

No comments yet | Read rest of this entry »

HTMLki

Seamless templating with the HTML spirit. Supports PHP 5.2 and up. Standalone.

HTMLki

HTMLki (htmlki.com) is a standarlone templating engine for PHP 5.2 and up. Unlike most today’s templating systems that treat HTML as text and insert their own data here and there HTMLki extends HTML by adding variables, loops, language lines, etc. directly into HTML constructs. At the same time any valid HTML or PHP code is a valid HTMLki template.

For example, here’s a simple menu in HTMLki:

This is equivalent to the following PHP code:

Features

«…»

No comments yet | Read rest of this entry »

QuickDiff for PHP

A tiny standalone in-line and by-line diffing class with built-in HTML output.

Once I needed a decent text diffing solution for SafePatch and after some searching couldn’t find one. So I’ve decided to write my own. It might not be very accurate but it’s very easy to use and has two modes – finding changes by line or inline (within one line). It also comes with built-in HTML generation although you need to style it yourself with CSS.

It has no dependencies and works out of the box in PHP 5.2 and up – simply include it and you’re ready to go.

Download QuickDiff here. Released in public domain. You can also check/fork/report issues to GitHub.

Class methods

«…»

No comments yet | Read rest of this entry »

strip_tags() is not a proper way of removing HTML

I have been using this function to retrieve text representation from a HTML string for quite a while. I believed it worked fine and so it did but I haven’t thought that it alone wasn’t enough: I have completely missed the point that strip_tags doesn’t remove HTML entities. Somehow I took it as granted but as it turns out that was wrong.

This is a better way of «formatting» HTML into plain text:

html_entity_decode will take care of xml&xxx;, xml{ and xmlģ converting them to real characters so that xmldo not break will look as «do not break».

Note: this function does its job as expected and treats non-breaking space (Unicode U+00A0) as a separate symbol, not a regular space (U+0020). That means that PHPtrim(html_entity_decode(' ')); won’t return an empty string – as mentioned in PHP docs. The same happened in the example above where «Woo» and «wee» are separated not by a regular dash (–) but rather a em-dash ().

Also note that UTF-8 is the default encoding beginning with PHP 5.4 – at first I didn’t read the changelog and was wondering why I was getting a Latin-1 string instead of UTF-8.

No comments yet | Show this entry »

Getting environment variable case-insensitively in PHP

This is a small tip and reminder for myself in case I forget this newly discovered method.

Typically (and it’s more performance-wise) one would read environment variables from PHP$_SERVER[]. However, arrays in PHP are case-sensitive and many variables, especially OS-related like COMSPEC or WINDIR, appear in different char case from system to system.

But there’s a function – getenv() – that ignores char case, at least on Windows:

Note that checking for OS is a quick but dirty trick and even if necessary one should consider using PHPPHP_OS, php_uname() or other means than environment variables.

No comments yet | Show this entry »

Batch syntax check of PHP scripts

xargs is a great little utility for FreeBSD systems that invoke a command per each line in stdin, or per each multiple lines there.

If we combine it with find we can run syntax check over all files in a directory or matching find’s criteria:

  • -L makes xargs run the command once per 1 input line
  • shphp -l <fn> runs syntax check

find has its own option for executing command per each match but I find xargs more intuitive to use.

No comments yet | Show this entry »

PHP Scripter

Standalone lightweight batch-like PHP script interpreter with support for functions, conditions and variables.

This script was written a long while ago – near April 2011 when I was launching i-Tools.org (in particular, it powers up its Text processor with more than 130 functions). I have several more interpreters written in other languages (e.g. in Delphi and JavaScript) that I hope to post one day as well.

Download

Download the class here: php-scripter-20121204.zip.

It has no dependcencies except for mbstring thus supporting Unicode scenarios. Released in public domain so you’re free to do whatever you want with it.

«…»

No comments yet | Read rest of this entry »