Archive for March, 2007

showing syntax highlighted source

Problem Want to display PHP code with syntax highlighting (colors). This helps with debugging, etc. Solution Within PHP you can nicely display colorful text, which can be very useful for debugging or just to showcase your code.We simply use the highlight_file function within PHP, like this below. Example <?php highlight_file("transform.php"); ?>For a real-life demo, take [...]

Useful PHP code – guestbook counter

Problem You want to display a counter on your web page, but you do not want to use a db. Solution All you need to make this work is echo “1″ into a file called counter and ensure the web user has write access. For example: $ echo 1 > counter; chmod 777 counter; Example [...]

opening file then reading into variable

Problem You want to open save the contents of a file, into a variable in PHP. Solution Simple demo on opening and reading contents of files in PHP.Script under example tab takes filename as a parameter, then opens it read-only and reads contents in data variable.Then it echoes a header and footer line, with the [...]

how to interperse HTML with PHP

Problem You want to intersperse HTML with PHP code. Solution Just getting back to basics, this is a very simple demoof how to implement PHP code along with HTML. Example <html><head>….<body><h1>Hello World</h1><?php if($_GET[runit]) { ?> <b>This will get displayed if ?runit=yes isappended to the web users request</b><?php } else { ?>nothing to see here … [...]

working with arrays part two

Problem You want to create an array, then traverse it. Solution Following up from part one – working with arrays.To spin through all elements in our names array, here is the code below. Example <?php$count=count($names);for ($i=0;$i<$count;$i++) { print($names[$i].”\n”); }?>Then for our string index array (hash):<?phpwhile(list($k,$v)=each($addresses)) { echo “$k lives at $v\n”;}?> Reference Technorati Tags: PHP [...]

working with arrays part one

Problem You want to work with string index arrays. Sometimes called Hashes. Solution PHP can handle number or string indexed arrays.Arrays can be initialized in a number of ways. For example:Basic array assignment and initialization, without specify numbers or strings:<?php$names[]=”joe”;$names[]=”john”;$names[]=”jim”;?> Example Specifically assigning values to numbered elements in the names array:<?php$names[0]=”joe”;$names[1]=”john”;$names[2]=”jim”;?>Quicker way to assign values [...]

regular expression with ereg and eregi

Problem You want to perform regular expression with PHP. Solution Again PHP implementation of regular expression is very similar to Perl.In the simpliest form, pattern matching:$ php -q<<EOT<?php$str=”hello world”;if(ereg(“world”,”$str”)) { echo(“got a match”);}?>EOTgot a match Example Similarly eregi operates the same, just it is case insensitive.$ php -q<<EOT<?php$str="hello world";if(eregi("WORLD","$str")) { echo("got a matchn");}?>EOTgot a match [...]

string manipulation strlen and chr

Problem You want to perform string manipulation with PHP. Solution Following on from the intro post, see the example tab for some more demos on PHP string manipulation. Example Counting the number of characters in a string:strlen$ php -q<<EOT<?phpecho(strlen(“Hello World”).”n”);?>EOT11Converts an ASCII code to a character:chr$ php -q<<EOT<?phpecho(chr(65).”n”);?>EOTA Reference Technorati Tags: PHP strlen, PHP chr, [...]

string manipulation substr and trim

Problem Substituting and trimming string variables in PHP. Solution Similar to Perl, PHP has an extensive library of functions.See the example tab for a few simple examples, showing the basic syntax. Example substr$ php -q<<EOT<?phpecho(substr(“Hello World”,0,4).”\n”);?>EOTHelltrim$ php -q<<EOT<?phpecho(trim(” Hello World “).”\n”);?>EOTHello World Reference Technorati Tags: PHP substr, PHP trim, PHP Coding SchoolPHP Docs substr FunctionPHP [...]