Optional $_GET argument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cheesecaker
    New Member
    • Feb 2007
    • 66

    #1

    Optional $_GET argument

    In this random practice script I was doing, I was passing some variables to the script with the URL, which means the $_GET method.

    [PHP]<?php

    header("Content-type: image/png");

    $textinput = $_GET['input'];

    if ($_GET['size'] == '')
    {
    $textsize = 24;
    }

    else
    {
    $textsize = $_GET['size'];
    }


    $height = $textsize + 30;
    $canvas = imagecreate(800 ,$height);
    $white = imagecoloralloc ate($canvas,255 ,255,255);
    $black = imagecoloralloc ate($canvas,0,0 ,0);;

    $ycoord = $height * (3/4);

    Imagettftext($c anvas,$textsize ,0,0,$ycoord,$b lack,'SEGOEUI.T TF',$textinput) ;

    imagepng($canva s);


    ?>[/PHP]

    It's a image generation script that outputs some text as an image. (Obviously)
    It works perfectly, all except for this problem:

    I want the $_GET['size'] argument to be optional. However, if you don't pass "&size=" to the script, it dies because of the undefined index of $_GET['size'].

    Basically, I want to make it optional so that if I don't pass it a size, it should just set the size to a default of 24 and continue on.
  • jx2
    New Member
    • Feb 2007
    • 228

    #2
    well it does work well on my server... (except for a font )

    i dont see any problems so it might be due to configration of your PHP

    if u believe that unspecify size cos truble u might try : if(!$_GET['size'])

    regards
    jx2

    Comment

    • ronnil
      Recognized Expert New Member
      • Jun 2007
      • 134

      #3
      you can always use isset().

      [CODE=php]<?php
      if(isset($_GET['size']))
      {
      $size = $_GET['size'];
      } else {
      $size = my_standard_siz e
      }
      ?>[/CODE]

      in other cases where you might wanna swallow a possible error, you can always use '@' in front of the variable/function. e.g. @$_GET['size'] won't produce an error even if there is one, I believe it returns null...?

      Comment

      Working...