Web Analytics Made Easy -
StatCounter Force $variable to be numeric or a real number? - CodingForum

Announcement

Collapse
No announcement yet.

Force $variable to be numeric or a real number?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Force $variable to be numeric or a real number?

    I have a varaible $MAXwh = 299;
    I am sure this and the others are right, yet the getimagesize doesn't like it, when it's dynamic.
    PHP Code:
    // First attempt...
    echo (!is_numeric($MAXwh))?"not a number"."<br />"$MAXwh."<br />"
    // Result= 299
     
    // Second attempt...
    if(!is_numeric($MAXwh)){
    echo 
    "not a number";
    }else{
    echo 
    $MAXwh;
    }
    // Result= 299
     
    // Third attempt...
    if (is_int($MAXwh)){
    echo 
    $MAXwh;
    }else{
    echo 
    "not a number";
    }
    // Result= 299
     
    // Last attempt...
    if (is_numeric($MAXwh)){
    echo 
    $MAXwh;
    }else{
    echo 
    "not a number";
    }
    // Result= 299 
    Anyway to force the $variable to be a number or numeric even though it is?

  • #2
    PHP Code:
    $maxwh intval($MAXwh); 
    http://www.php.net/manual/en/function.intval.php

    best regards

    Comment


    • #3
      PHP is datatype weak, so that means you'll need explicit casting for this. Most functions are sensitive enough to handle strings that can be interpreted as numbers, but they are not a requirement when writing you're functions and methods.
      So, the first thing to do is determine if you want an integer or a number. An integer does not allow an exponent on it, and its actually a pain to determine:
      PHP Code:
      if (is_numeric($input))
      {
          
      // We can identify it as a number:
          // Now, to determine if its an integer:
          
      $tmp = (int)$input;
          if (
      $tmp == $input)
          {
              
      // It is an integer, write it over or cast
              
      $input $tmp;
          }
          else
          {
              print 
      'Provided input is not an integer!';
          }
      }

      // Or, the easy route, using ctype:
      if (!empty($input) && ctype_digit($input)) // Empty check is required up to 5.1
      {
          
      $input = (int)$input;
      }
      else
      {
          print 
      "Provided input is not an integer!";

      Sadly, is_int actually looks for an integer datatype, and will not interpret a string as an integer.
      PHP Code:
      header('HTTP/1.1 420 Enhance Your Calm'); 
      Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

      Comment


      • #4
        Ok the funny thing is that I have a function make_thumb
        If I add this $MAXwh = 299 after the text below it works.
        PHP Code:
        function make_thumb($img_name,$filename,$new_w,$new_h){ 
        If I add it above, it doesn't work, very odd?

        Comment


        • #5
          No.
          Above that line puts it into a scope outside of the make_thumb, while below that line puts it into a local scope in the function make_thumb. If you put that inside the function, one of you're parameters will be useless, assuming its supposed to be dynamically passed in. The problem is you're not passing in the correct value to the function; if its requiring an integer there will be a call inside the function somewhere that states an is_int($x) in it.
          PHP Code:
          header('HTTP/1.1 420 Enhance Your Calm'); 
          Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

          Comment


          • #6
            This is the entire make_thumb function.

            PHP Code:
            // ****************
            //IF I ADD IT HERE IT WONT WORK.
            $MAXwh 299;
            // ****************
            function make_thumb($img_name,$filename,$new_w,$new_h){
             
            // ****************
            //IF I ADD IT HERE IT WORKS.
            $MAXwh 299;
            // ****************
             
            //get image extension.
            $ext=getExtension($img_name);
            //create a new image using the appropriate function from gd library
            if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
            $src_img=imagecreatefromjpeg($img_name);
            if(!
            strcmp("png",$ext))
            $src_img=imagecreatefrompng($img_name);
            if(!
            strcmp("gif",$ext))
            $src_img=imagecreatefromgif($img_name);
            $tmpName $img_name;
             
            //check the image size with getimagesize
            list($width$height$type$attr) = getimagesize($tmpName);
            if(
            $width <= $MAXwh|| $height <= $MAXwh){
            //die("exceeded image dimension limits.");
            //use the image width and height, DON'T CHANGE THEM!
            $old_x=imagesx($src_img);
            $old_y=imagesy($src_img);
            $thumb_h=$old_y;
            $thumb_w=$old_x;
            }else{
            //gets the dimmensions of the image
            $old_x=imagesx($src_img); //width
            $old_y=imagesy($src_img); //height
            $ratio1=$old_x/$new_w;
            $ratio2=$old_y/$new_h;
            if(
            $ratio1>$ratio2){
            $thumb_w=$new_w;
            $thumb_h=$old_y/$ratio1;
            }else{
            $thumb_h=$new_h;
            $thumb_w=$old_x/$ratio2;
            }

            Edit: I created this in early hours of the morning on Tuesday at about 2:42am, it isn't the best but it works.
            Somehow the numeric $variable is getting lost if it's added above the function?
            I guess my question now is,
            Is there a way to make it $GLOBAL and the $MASTER value that can be used anywhere in the page if added right at the top?
            Last edited by student101; Apr 3, 2009, 03:50 AM.

            Comment


            • #7
              Basically 'cause it's a function you can only do things inside the functions specified area between the { and }
              Pretty cr** but hey, what can I do?

              Comment


              • #8
                As Fou-Lou says in the post above yours, putting $MAXwh above the function declaration doesn't work because the variable is outside the scope of the function (ie $MAXwh doesn't exist inside the function if it is declared outside the function). If you NEED to declare it outside the function AND it is a variable value not a constant then you need to add this line inside your function
                global $MAXwh;
                http://www.hazelryan.co.uk

                Comment


                • #9
                  Originally posted by student101 View Post
                  Basically 'cause it's a function you can only do things inside the functions specified area between the { and }
                  Pretty cr** but hey, what can I do?
                  ...um learn programming 101
                  http://www.hazelryan.co.uk

                  Comment


                  • #10
                    Originally posted by NancyJ View Post
                    If you NEED to declare it outside the function AND it is a variable value not a constant then you need to add this line inside your functionglobal $MAXwh;
                    Yes I tried similar, still as odd as it is - didn't work
                    Mine was globals[$MAXwh], your GLOBAL method works.
                    I am sure I tried that one to, too much code no coffee.

                    Edit:
                    Originally posted by NancyJ View Post
                    ...um learn programming 101
                    Haha, I just turned 30, my chick says it's all down hill now - I can't wait till I get to 18...

                    Thank you all, I appreciate the time.
                    Last edited by student101; Apr 3, 2009, 05:52 AM.

                    Comment


                    • #11
                      You really should avoid globalization though, it kills off compatibility for extension scripts and can be a pain with debugging.
                      Instead, simply resignature you're function to include the $MAXwh and add it as an argument when you call the function.
                      PHP Code:
                      header('HTTP/1.1 420 Enhance Your Calm'); 
                      Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                      Comment


                      • #12
                        Originally posted by Fou-Lu View Post
                        You really should avoid globalization though, it kills off compatibility for extension scripts and can be a pain with debugging.
                        Instead, simply resignature you're function to include the $MAXwh and add it as an argument when you call the function.
                        You mean like this?
                        PHP Code:
                         function make_thumb($img_name,$filename,$new_w,$new_h,$MAXwh){ 

                        Comment


                        • #13
                          Correct.
                          Then when you call it, you just add in the last parameter. If you wanted to, you could specify a default value for $MAXwh:
                          PHP Code:

                          function make_thumb($img_name$filename$new_w$new_h$MAXwh 399)
                          {
                          ... 
                          Too bad PHP doesn't support overloading.
                          PHP Code:
                          header('HTTP/1.1 420 Enhance Your Calm'); 
                          Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                          Comment


                          • #14
                            Ok seems promising, but if a user doesn't know PHP and wants to buy the upload script they have to add their value in. The script currently has all the $vars at the top so the user doesn't have change (damage) the code.
                            Like If they need an image to be set at 350px in width then it becomes...
                            PHP Code:
                             function make_thumb($img_name$filename$new_w$new_h$MAXwh 350

                            Edit: Wow, I thought I knew enough to get me by (many ways to do it), clearly I need to read / learn more.

                            Last edited by student101; Apr 3, 2009, 06:13 AM.

                            Comment


                            • #15
                              You're planning on selling the script?

                              If this is a configurable value not a variable you should use constants, they are available in global scope.
                              http://www.hazelryan.co.uk

                              Comment

                              Working...
                              X