Run PHP from the Command Line

Testing in a browser is handy for seeing HTML output, but a lot of other things are lost. If you run your script from the command line you can see whether or not PHP is "fixing" your code for you, particularly in the case of uninitiated variables that PHP sometimes handles on its own.

    5 days later

    Isn't that what E_STRICT is for... to make PHP throw an error for every uninitialized variable instead of doing 'magic' and producing a value on its own?

      When using (uninitialized) variables that do not exist, errors of level E_NOTICE are produced and you cannot stop the PHP voodoo magic (like making $foo[bar] turn bar into 'bar'), it happens, but to go along with the magic you can produce (and find) the errors if (when) you turn them on.

      People are encouraged to log all errors instead of using display_errors = on but regardless the main goal here (on this specific subtopic of eliminating php errors) is to turn on ALL errors, test the code, and eliminate said errors.

      error_reporting(E_ALL | E_STRICT);

      http://php.net/error-reporting
      http://php.net/manual/ref.errorfunc.php#e-strict
      http://php.net/set-error-handler

      Wow, this thread is old :-)

        a year later

        I find print_r() very useful whiledebuging.

        $a = /*???*/; If you're not 100% on what a variable/array contains
        echo "<pre>";
        print_r ($a);
        echo "</pre>";

        The out put will be quite understandable.

        Edit: wops this hint was allready posted. Sorry!

        However, as an adition to print_r you can use print_r($var, true); so rather than displaying the output it returns it as a string.

        This can be then used in an error log and is often very useful.

          Don't forget about [man]debug_backtrace/man and [man]debug_print_backtrace/man.

            2 years later

            I'm looping through a address list sending the $address to the file_get_contents().

            Is there a way I can get the HTTP header response from a file_get_contents()?
            I'm geocoding yahoo maps. If there is a bad address an error prints on the map.
            I want to store the error in an array with the address. But I don't know how to get the HTTP response to the request????? Line 85 is the $addressData line in the function.
            thanks,
            ------Yahoo error message resonse if there is a bad address--------
            Warning: file_get_contents(http://local.yahooapis.com/MapsService/V1/geocode?appid=JezVqMLV34G_VJeKk_o8kM12GbWa1PiTotJPt6OxwowFgMb73vEJ.VUdXXp92w--&location=) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /Library/WebServer/Documents/phoogle1a.php on line 85

            ---------------------------- function
            function addAddress($address,$htmlMessage=null){
            if (!is_string($address)){
            die("All Addresses must be passed as a string");
            }
            $apiURL = "http://local.yahooapis.com/MapsService/V1/geocode?appid=JezVqMLV34G_VJeKk_o8kM12GbWa1PiTotJPt6OxwowFgMb73vEJ.VUdXXp92w--&location=";
            $addressData = file_get_contents($apiURL.urlencode($address));

              a month later

              These simple functions are a big help while developing. Set the DEBUG function to false when in production.

              <html>
              <head>
              <title>Debug Output</title>
              <body>
              
              <?php
                 arrayLog(DEBUG(), "This is Post", $_POST);
                 outputLog(DEBUG(), "This is txtOne", $_POST['txtOne']);
              ?>
              
              <form name="frm1" action="#" method="post">
                 <input type="text" name="txtOne" />
                 <input type="submit" name="isSubmit" value="Submit" />
              </form>
              </body>
              </head>
              
              </html>
              <?php
              function DEBUG(){
                 return true;
              }
              
              function arrayLog($debug, $msg, $theArray){
                 if($debug){
                    echo "<pre>".$msg.": ";
                    print_r($theArray);
                    echo "</pre>";
                 }
              }
              
              function outputLog($debug, $msg, $output){
                 if($debug){
                    echo "<br />".$msg.": ".$output."<br />";
                 }
              }
              
              ?>
              
              
                reddrum wrote:

                These simple functions are a big help while developing. Set the DEBUG function to false when in production.

                It might be easier to use a defined constant instead of a function.

                  Improved version:

                     define("DEBUG", true);
                  
                     function outputLog($debug, $msg, $output){
                        if($debug){
                           if(is_array($output)){
                              echo "<pre>".$msg.": ";
                              print_r($output);
                              echo "</pre>";
                           }else{
                              echo "<br />".$msg.": ".$output."<br />";
                           }
                        }
                     }
                  
                  
                    5 months later
                    a year later

                    Comments

                    It might be one of the most basic things, but I think it is worth a mention in a Debugging 101 thread:

                    Use comments as much as possible to explain what you are doing with the code and why. Your code might look crystal clear to you now, but if you go back to that same piece of code 5 months later, you might not see immediately why you wrote the code the way you did. It can also help if you need to do a search within your code for a specific section.

                    I can't tell you how many times I've gone back to a file months after the fact and if it hadn't been for my comments it would have taken me an hour or two just to figure our why I wrote the page that way.

                      7 years later
                      a year later
                      Write a Reply...