showing syntax highlighted source

[problem]

Want to display PHP code with syntax highlighting (colors). This helps with debugging, etc.

[/problem]

[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.

[/solution]

[example]

<?php highlight_file(“transform.php”); ?>

For a real-life demo, take a look here:

[/example]

[reference]

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

[/reference]

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.

[/problem]

[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;

[/solution]

[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]

[reference]

[tags]PHP Guest Book, PHP Coding School[/tags]

[/reference]

opening file then reading into variable

[problem]

You want to open save the contents of a file, into a variable in PHP.

[/problem]

[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.

[/solution]

[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 ######

[/example]

[reference]

[tags]PHP file handling, PHP Coding School[/tags]

[/reference]

how to interperse HTML with PHP

[problem]

You want to intersperse HTML with PHP code.

[/problem]

[solution]

Just getting back to basics, this is a very simple demo
of how to implement PHP code along with HTML.

[/solution]

[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>

[/example]

[reference]

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

[/reference]

working with arrays part two

[problem]

You want to create an array, then traverse it.

[/problem]

[solution]

Following up from part one – working with arrays.

To spin through all elements in our names array, here is the code below.

[/solution]

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

?>

[/example]

[reference]

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

[/reference]

working with arrays part one

[problem]

You want to work with string index arrays. Sometimes called Hashes.

[/problem]

[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";

?>

[/solution]

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

?>

[/example]

[reference]

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

[/reference]

regular expression with ereg and eregi

[problem]

You want to perform regular expression with PHP.

[/problem]

[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

[/solution]

[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

[/example]

[reference]

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

[/reference]

string manipulation strlen and chr

[problem]

You want to perform string manipulation with PHP.

[/problem]

[solution]

Following on from the intro post, see the example tab for some more demos on PHP string manipulation.

[/solution]

[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

[/example]

[reference]

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

[/reference]

string manipulation substr and trim

[problem]

Substituting and trimming string variables in PHP.

[/problem]

[solution]

Similar to Perl, PHP has an extensive library of functions.

See the example tab for a few simple examples, showing the basic syntax.

[/solution]

[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

[/example]

[reference]

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

[/reference]