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 a look here:



Reference

[tags]PHP Syntax Highlighting, PHP Coding School[/tags]



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


<?php

$fh=fopen(“/tmp/counter.txt”, “r”);
$cnt=fread($fh,filesize(“/tmp/counter.txt”));
fclose($fh);
$fh=fopen(“/tmp/counter.txt”, “w+”);
if(fputs($fh,($cnt+1))) { print(“You are visitor: $cntn”); }
fclose($fh);

?>


[/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 data in the middle.



Example


<?php

$filename=$argv[1];
$FH=fopen("$filename","r") or die("could not open $filenamen");

while(!feof($FH)) { $data.=fgets($FH); }

fclose($FH);

echo "###### beginning of $filename ######n";
echo "$data";
echo "n###### end of $filename ######n";

exit(0);

?>

Here is a run through:


$ php -q readfiles.php rhyme.txt
###### beginning of rhyme.txt ######
Mary had a little lamb,
It was always bleating.
###### end of rhyme.txt ######





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 demo
of 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 is
appended to the web users request</b>

<?php } else { ?>

nothing to see here ... :)

<?php } ?>

<body><html>



Reference

[tags]Interperse HTML in PHP, PHP Coding School[/tags]



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):

<?php

while(list($k,$v)=each($addresses)) {
echo "$k lives at $vn";
}

?>



Reference

[tags]PHP Arrays, PHP Hashes, PHP Coding School[/tags]



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 to names array:

$names=array("joe","john","jim");

Assigning values to a string indexed array – or hash (key and value pairs):

<?php

$addresses=array(
"joe" => "1 Marble Street",
"john" => "12 Marble Street",
"jim" => "23 Marble Street"
);

?>



Reference

[tags]PHP, Arrays, PHP Coding School[/tags]



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");
}

?>
EOT

got 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");
}

?>
EOT

got a match



Reference

[tags]PHP ereg, PHP eregi, PHP Coding School[/tags]



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
<?php

echo(strlen("Hello World")."n");

?>
EOT

11

Converts an ASCII code to a character:

chr

$ php -q<<EOT
<?php

echo(chr(65)."n");

?>
EOT

A



Reference

[tags]PHP strlen, PHP chr, PHP Coding School[/tags]



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
<?php

echo(substr("Hello World",0,4)."n");

?>
EOT

Hell

trim

$ php -q<<EOT
<?php

echo(trim(" Hello World ")."n");

?>
EOT

Hello World



Reference

[tags]PHP substr, PHP trim, PHP Coding School[/tags]