View Single Post
Old 02-21-2013, 07:49 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yeah, you can give it a shot I didn't test it. Works alright in my head though
If you aren't learning about file handling or anything like that, then this can be done much easier:
PHP Code:
<?php

$sFile 
__DIR__ '/quotes.txt';
if (
false !== ($aQuotes file($sFileFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES)))
{
    print 
$aQuotes[array_rand($aQuotes1)];
}
else
{
    print 
'Cannot retrieve quotes!';
}
__FILE__ differs in that its the entire path including "this" script. Prior to. . . 5.2.x I think it was, __DIR__ hadn't existed, so we would use dirname(__FILE__) in its place.
It is IMO wise to always include or open files relative to "this" script. Since you can include "this" script in a different one with a different path, PHP takes its cwd as that of the executing script, not the inclusions. Using relatives from __DIR__ will always produce the correct paths.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote