Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

PHP manual gems

Name: Anonymous 2010-01-04 0:47

Every now and then, one comes across these mind-blowingly profound comments in the PHP manual. Post your discoveries here!

I'll start with this: (from http://www.php.net/manual/en/function.abs.php#94768)

Here is a simple function to make positives to negative and negatives to positive. This is function:

<?php
function turn($x)
     {
     $y = abs($x);
     if ($y == $x)  
         return "-$y";  
     else      
         return "$y";  
     }
?>


You can use this code:

<?php
echo turn(-5) + turn(10);
?>


The output is:

-5

;
Because 5 + -10 = -10

Name: Anonymous 2010-01-04 0:49

>>1
... That's amazingly stupid. What else could I expect of PHP user ``contributions''.

Name: ANIME JONES 2010-01-04 0:51

I give this thread <?php echo turn(-10) + turn(5);?> out of 5 stars

Name: Anonymous 2010-01-04 1:05

Surely this is an elaborate troll. /prog/ could learn a thing or two from them... or submit your own perhaps?

Name: Anonymous 2010-01-04 1:09

At the top of your page, do something to this effect:
<?php
    $n = "\n";
    $t = "\t";
?>

Then, if you need your table cell four tabs in:

<?php echo($t . $t . $t . $t . '<td>whatever</td>' . $n); ?>

This means the parser only has to interpret four characters inside double quotes, then just stores them in variables.  With strings that small, concatenating six things together won't be slow at all.


(http://www.php.net/manual/en/function.print.php#71189)

Name: Anonymous 2010-01-04 1:19

>>1
OK, I raged. Now please tell me that this isn't real.

Name: Anonymous 2010-01-04 2:29

There's nothing wrong with this code…

Gautam
30-Aug-2007 08:56


<?php
//EXAMPLE  of Multi-Dimentional Array where as an array's keys are an array itself.
//It's so easy to create one like this.

$movie_to_watch = array ('Action'=>
          array('Kanu Reeves' => 'Matrix Reloaded',
                      'Pearce Brosnan' => 'Die Another Day',
                      'Tom Cruz' => 'Mission Impossible',
                      'Jason Statham' => 'Crank',
                      'Danzel Washington' => 'Man on Fire'),
                'Comedy' =>
                array ('Charlie Chaplin' => 'City Lights',
                       'Jim Carrey'    => 'Cable Guy',
                       'Rowan Atkinson' => 'The Ultimate Disaster'));
$type_wanted = 'Action'; //You may switch type from Action to Comedy.
$hero_wanted = 'Pearce Brosnan'; // You may switch hero from Pearce Brosnan to Jim Carrey.

print ("$hero_wanted 's  $type_wanted movie is " . $movie_to_watch[$type_wanted][$hero_wanted].".");
// produces browser output as under:
// Pearce Brosnan 's Action movie is Die Another Day.
?>


(http://us3.php.net/manual/en/language.types.array.php#77473)

Name: Anonymous 2010-01-04 2:49

>>5
HAHAHA
11/10 for sure.

Name: Anonymous 2010-01-04 4:05

>>1
wat

Name: Anonymous 2010-01-04 4:51

>>1
The rest of the user posts on that page are equally entertaining.

Fortunately, I usually refer to the offline version, which lacks the Youtubish comments.

Name: Anonymous 2010-01-04 12:53

>>4
That is an excellent idea. Prizes for the best submission.

Name: Anonymous 2010-01-04 18:32

http://www.php.net/manual/en/function.fopen.php#94643

This is a function for reading or writing to a text file...
The function has three modes, READ, WRITE, and READ/WRITE... READ, returns the text of a file, or returns FALSE if the file isn't found. WRITE creates or overrides a file with the text from the $input string, WRITE returns TRUE or FALSE depending on success or failure. READ/WRITE reads a text file, then writes the string from $input to the end of the file, like WRITE, it returns TRUE or FALSE depending on success or failure.


<?php
function openFile($file, $mode, $input) {
    if ($mode == "READ") {
        if (file_exists($file)) {
            $handle = fopen($file, "r");
            $output = fread($handle, filesize($file));
            return $output; // output file text
        } else {
            return false; // failed.
        }
    } elseif ($mode == "WRITE") {
        $handle = fopen($file, "w");
        if (!fwrite($handle, $input)) {
            return false; // failed.
        } else {
            return true; //success.
        }
    } elseif ($mode == "READ/WRITE") {      
        if (file_exists($file) && isset($input)) {
            $handle = fopen($file "r+");
            $read = fread($handle, filesize($file));
            $data = $read.$input;
            if (!fwrite($handle, $data)) {
                return false; // failed.
            } else {
                return true; // success.
            }
        } else {
            return false; // failed.
        }
    } else {
        return false; // failed.
    }
    fclose($handle);
}
?>
EXAMPLE CALLS:
<php
openFile2("files/text.txt", "WRITE", "Hello World!");
echo openFile2("files/text.txt", "READ"); // OUTPUT > Hello World!
openFile2("files/text.txt", "READ/WRITE", "!!");
echo openFile2("files/text.txt", "READ"); // OUTPUT > Hello World!!!
?>

Name: Anonymous 2010-01-04 18:39

x *= -1
OPTIMIZED that for you, OP;

Name: Anonymous 2010-01-04 18:41

>>12
Finder of this "gem" here. I know we all have to learn some time, but spraying it all over the Internet is a decidedly bad idea.

I seriously had to take off my glasses, rub my eyes, and then go two inches from my monitor to verify that this was, in fact, what I was seeing. I'm.. leaving the Internet for a few days to recover. My god.

Name: Anonymous 2010-01-04 18:47

>>12
why would you do this?
what kind of maniac would write a dozen lines in order to reduce two lines of code to one?
not to mention the obvious performance hit caused by doing this

Name: Anonymous 2010-01-04 18:55

>>15
EXPERT ENTERPRISE PHP PROGRAMMERS

Name: Anonymous 2010-01-04 19:25

Results 1 - 10 of about 3,750 from php.net for "else return" -intitle:"PHP Bugs". (0.07 seconds)

http://www.google.ca/search?q=site%3Aphp.net+%22else+return%22+-intitle%3A%22PHP+Bugs%22

Name: Anonymous 2010-01-04 19:34

>>13
just writing -$var works fine too

Name: Anonymous 2010-01-04 21:02

>>18
Oh really? Because I'm sure /prog/ couldn't have figured that out. That wasn't the joke or anything.

Name: Anonymous 2010-01-04 22:59

>>19
actually, that was the joke. you should give prog more credit

Name: Anonymous 2010-01-05 12:05

>>12
MAMA COOKING STOP.
Why would someone do something like that?

Name: Anonymous 2010-01-05 16:38

>>15,21
I could see wanting to reduce checking for each read/write into a function that returns a status and you can do processing based on the status rather than doing numerous checks; however, this is the worst possible implementation. Simply awful.

Name: Anonymous 2010-01-05 16:53

look on line 11 of shiichan

Name: Anonymous 2010-01-05 22:45

>>23
if ($_SERVER[REQUEST_METHOD] != 'GET') fancydie('I POSTed your mom in the ass last night.'); ??

Name: Anonymous 2010-01-14 17:45

http://www.php.net/manual/en/ref.session.php

annbainbridge81_at_gmail_dot_com
16-Nov-2008 07:03
-------------------------------------------------------------
So after like six hours of debugging I found that echoing the word “dick” some how kills a session. Like story short was testing retrieval from a data base and had used Tom, Dick and Harry as test names.  I have found that dick kill my session even if use in just a echo statement  ex( echo “dick”;) has anyone else ran into this and if so is there a way around it.

Name: Anonymous 2010-01-14 18:06

>>25
This is exactly the sort of thing I'd do if I created a programming language.

Name: Anonymous 2010-11-15 17:15

Don't change these.
Name: Email:
Entire Thread Thread List