|
|
Note: The archives category content is an automatically generated focus channel and does not neccessarily reflect the opinions of this blog. No responsibility is taken for the external links presented here, follow at your own discretion. The archives content is never scraped from sites - but an abstract obtained from search engines.
Category: 'PHP Coding Training'
Monday, March 26th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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.
|
|
No Comments »
Friday, March 16th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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;
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
<?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); ?>
|
No Comments »
Thursday, March 15th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
<?php $filename=$argv[1]; $FH=fopen("$filename","r") or die("could not open $filename\n"); 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 ######
|
No Comments »
Wednesday, March 14th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
<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>
|
No Comments »
Wednesday, March 14th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
<?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 $v\n"; } ?>
|
No Comments »
Wednesday, March 14th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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”; ?>
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
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" ); ?>
|
No Comments »
Wednesday, March 14th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
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
|
No Comments »
Tuesday, March 13th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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
|
No Comments »
Tuesday, March 13th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
Similar to Perl, PHP has an extensive library of functions. See the example tab for a few simple examples, showing the basic syntax.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
substr $ php -q<<EOT <?php echo(substr("Hello World",0,4)."\n"); ?> EOT Hell trim $ php -q<<EOT <?phpecho(trim(" Hello World ")."\n"); ?> EOT Hello World
|
No Comments »
|