orgalorg php irc bot

This bot was the result of an afternoon's play. It is essentially a memory leak wrapped around some networking. It isn't complete, and probably won't be completed, since I don't think this is the correct approach for an IRC bot in PHP.

<?php

// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);

// Change these values!
$channels  = array(...);
$nickname = 'bot-name';
$password = 'password';
$operant_nickname   = 'authors_irc_name';

// Opening the socket to the Rizon network
$socket = fsockopen("irc.freenode.net", 6667);

// Send auth info
fputs($socket, "PASS " . $password . "\n");
fputs($socket, "NICK " . $nickname . "\n");
fputs($socket, "USER " . $nickname . " 0 * :" . $operant_nickname . "'s Bot\n");

// Join channel
foreach($channels as $channel) {
    fputs($socket, "JOIN " . $channel . "\n");
}

// Force an endless while
while (1) {
    // Continue the rest of the script here
    while ($data = fgets($socket, 128)) {
        echo $data;
        flush();

        // Separate all data
        $ex = explode(' ', $data);

        // Send PONG back to the server
        if ($ex[0] == "PING") {
            fputs($socket, "PONG " . $ex[1] . "\n");
        }

        // executes chat command
        if ($ex[0] != 'PING' && ISSET($ex[3])) {

            $command = str_replace(array(chr(10),chr(13)),'', $ex[3]);

            if ($command == ":!alive?") {
                fputs($socket, "PRIVMSG " . $ex[2] . " :Yo! I am operational. \n");
            }

            if ($command == ":!time") {
                fputs($socket, "PRIVMSG " . $ex[2] . " :" . date(DATE_RFC2822) . " \n");
            }

            if ($command == ":!help") {
                fputs($socket, "PRIVMSG " . $ex[2] . " :!alive?, !time, !operant?, !battery-staple, !uuid, !tell \n");
            }

            if ($command == ":!battery-staple") {
                $battery_staple = file_get_contents('https://miscellany.smallandnearlysilent.com/battery-staple/');
                fputs($socket, "PRIVMSG " . $ex[2] . " :" . $battery_staple . " \n");
            }

            if ($command == ":!uuid") {
                $uuid = file_get_contents('https://miscellany.smallandnearlysilent.com/uuid/');
                fputs($socket, "PRIVMSG " . $ex[2] . " :" . $uuid . " \n");
            }

            if ($command == ":!tell") {
                // TODO: Need some way to retrieve the from_person's handle.
                $user_input = preg_split('/(:!tell)/', $data);
                $words_from_user_input = str_word_count($user_input[1], 1);
                $user_name_to_tell = $words_from_user_input[0];
                $dirty_msg_txt = str_replace(array("\r\n","\r"),"",$user_input[1]);
                $regex_str = '/(' . $user_name_to_tell . ' ' . ')/';
                $clean_msg_txt_array = preg_split($regex_str, $dirty_msg_txt);
                $clean_msg_txt = $clean_msg_txt_array[1];
                fputs($socket, "PRIVMSG " . $ex[2] . " :I will tell, " . $user_name_to_tell . ", that you said: \"" . $clean_msg_txt . "\" the next time they check in with me. \n");
            }

            if ($command == ":!told") {
                // check for tells
            }

            if ($command == ":!operant?") {

                $parts = explode("!", $ex[0]);
                $user  = substr($parts['0'], 1);

                if ($user == $operant_nickname) {
                    fputs($socket, "PRIVMSG " . $ex[2] . " :Yes, " . $operant_nickname . ", you are the operant. \n");
                } else {
                    fputs($socket, "PRIVMSG " . $ex[2] . " :No, " . $user . ", you are not the operant. \n");
                }
            }

            if ($command = ":!finalcountdown") {
                $i = 10;
                while ($i > 1) {
                    fputs($socket, "PRIVMSG " . $ex[2] . ": " . $i . " \n");
                    $i = $i - 1;
                }
            }

            if ($command == ":!echo") {
                $echo_this = preg_split('/(:!echo)/', $data);
                fputs($socket, "PRIVMSG " . $ex[2] . ": " . $echo_this[1] . " \n");
            }

            if ($command == ":!dump") {
                fputs($socket, "PRIVMSG " . $ex[2] . " :DUMP THE GUTS " . implode(",",array_slice($ex, 4)) . " \n");
            }

            if ($command == ":!test") {
                fputs($socket, "PRIVMSG " . $ex[2] . " :value0 " . $ex[0] . ", value1 " . $ex[1] . ",value2 " . $ex[2] . ",value3 " . $ex[3] . "\n");
            }

        }
    }
}