Is there a shortcut i'm missing?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lo'oris

    #1

    Is there a shortcut i'm missing?

    $name=$_GET['name'];
    if (!$name)
    $name="value";

    i can't figure out how to shorten this thing. Is there some kind of
    operator i don't know about?

  • Jerry Stuckle

    #2
    Re: Is there a shortcut i'm missing?

    Lo'oris wrote:
    $name=$_GET['name'];
    if (!$name)
    $name="value";
    >
    i can't figure out how to shorten this thing. Is there some kind of
    operator i don't know about?
    >
    $name = isset($_GET['name']) ? $_GET['name'] : null;

    You should always test with isset() to see if a value passed to your
    page is set or not. Otherwise you will get a notice if you have them
    enabled.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Christoph Burschka

      #3
      Re: Is there a shortcut i'm missing?

      Jerry Stuckle wrote:
      Lo'oris wrote:
      >$name=$_GET['name'];
      >if (!$name)
      > $name="value";
      >>
      >i can't figure out how to shorten this thing. Is there some kind of
      >operator i don't know about?
      >>
      >
      $name = isset($_GET['name']) ? $_GET['name'] : null;
      >
      You should always test with isset() to see if a value passed to your
      page is set or not. Otherwise you will get a notice if you have them
      enabled.
      >
      If setting multiple variables from $_GET, you can also try this:

      $parameters=arr ay('name'=>"val ue",'example'=> "value1",'anoth er'=>"value2");
      foreach ($parameters as $parameter=>$va lue)
      {
      $$parameter=$_G ET[$parameter]?$_GET['parameter:$val ue;
      }

      --
      cb

      Comment

      • Jerry Stuckle

        #4
        Re: Is there a shortcut i'm missing?

        Christoph Burschka wrote:
        Jerry Stuckle wrote:
        >Lo'oris wrote:
        >>$name=$_GET['name'];
        >>if (!$name)
        >> $name="value";
        >>>
        >>i can't figure out how to shorten this thing. Is there some kind of
        >>operator i don't know about?
        >>>
        >$name = isset($_GET['name']) ? $_GET['name'] : null;
        >>
        >You should always test with isset() to see if a value passed to your
        >page is set or not. Otherwise you will get a notice if you have them
        >enabled.
        >>
        >
        If setting multiple variables from $_GET, you can also try this:
        >
        $parameters=arr ay('name'=>"val ue",'example'=> "value1",'anoth er'=>"value2");
        foreach ($parameters as $parameter=>$va lue)
        {
        $$parameter=$_G ET[$parameter]?$_GET['parameter:$val ue;
        }
        >
        Which is only slightly less dangerous than running with register_global s
        on. Someone can come in and set any variable in your script by setting
        it in the query string. And if you miss initializing a variable you've
        got a huge potential security breach.

        One reason register_global s is no longer enabled by default.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Hendri Kurniawan

          #5
          Re: Is there a shortcut i'm missing?

          Jerry Stuckle wrote:
          Christoph Burschka wrote:
          >Jerry Stuckle wrote:
          >>Lo'oris wrote:
          >>>$name=$_GE T['name'];
          >>>if (!$name)
          >>> $name="value";
          >>>>
          >>>i can't figure out how to shorten this thing. Is there some kind of
          >>>operator i don't know about?
          >>>>
          >>$name = isset($_GET['name']) ? $_GET['name'] : null;
          >>>
          >>You should always test with isset() to see if a value passed to your
          >>page is set or not. Otherwise you will get a notice if you have them
          >>enabled.
          >>>
          >>
          >If setting multiple variables from $_GET, you can also try this:
          >>
          >$parameters=ar ray('name'=>"va lue",'example'= >"value1",'anot her'=>"value2") ;
          >>
          >foreach ($parameters as $parameter=>$va lue)
          >{
          > $$parameter=$_G ET[$parameter]?$_GET['parameter:$val ue;
          >}
          >>
          >
          Which is only slightly less dangerous than running with register_global s
          on. Someone can come in and set any variable in your script by setting
          it in the query string. And if you miss initializing a variable you've
          got a huge potential security breach.
          >
          One reason register_global s is no longer enabled by default.
          >
          Not really. It think it's a clever way to do it. Save you some coding time.

          If you see, he only allows the variable in the parameter to be changed.

          But then again, it's only my oppinion.

          Hendri

          Comment

          • Jerry Stuckle

            #6
            Re: Is there a shortcut i'm missing?

            Hendri Kurniawan wrote:
            Jerry Stuckle wrote:
            >Christoph Burschka wrote:
            >>Jerry Stuckle wrote:
            >>>Lo'oris wrote:
            >>>>$name=$_G ET['name'];
            >>>>if (!$name)
            >>>> $name="value";
            >>>>>
            >>>>i can't figure out how to shorten this thing. Is there some kind of
            >>>>operator i don't know about?
            >>>>>
            >>>$name = isset($_GET['name']) ? $_GET['name'] : null;
            >>>>
            >>>You should always test with isset() to see if a value passed to your
            >>>page is set or not. Otherwise you will get a notice if you have them
            >>>enabled.
            >>>>
            >>>
            >>If setting multiple variables from $_GET, you can also try this:
            >>>
            >>$parameters=a rray('name'=>"v alue",'example' =>"value1",'ano ther'=>"value2" );
            >>>
            >>foreach ($parameters as $parameter=>$va lue)
            >>{
            >> $$parameter=$_G ET[$parameter]?$_GET['parameter:$val ue;
            >>}
            >>>
            >>
            >Which is only slightly less dangerous than running with
            >register_globa ls on. Someone can come in and set any variable in your
            >script by setting it in the query string. And if you miss
            >initializing a variable you've got a huge potential security breach.
            >>
            >One reason register_global s is no longer enabled by default.
            >>
            >
            Not really. It think it's a clever way to do it. Save you some coding time.
            >
            If you see, he only allows the variable in the parameter to be changed.
            >
            But then again, it's only my oppinion.
            >
            Hendri
            Not at all. I key in

            http://www.example.com?admin=1

            And now in your program $admin=1. And what if $admin is the variable
            which indicates I'm an admin?

            This effectively does exactly what register_global s does - just limits
            it to the $_GET variables.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Hendri Kurniawan

              #7
              Re: Is there a shortcut i'm missing?

              Jerry Stuckle wrote:
              Hendri Kurniawan wrote:
              >Jerry Stuckle wrote:
              >>Christoph Burschka wrote:
              >>>Jerry Stuckle wrote:
              >>>>Lo'oris wrote:
              >>>>>$name=$_GE T['name'];
              >>>>>if (!$name)
              >>>>> $name="value";
              >>>>>>
              >>>>>i can't figure out how to shorten this thing. Is there some kind of
              >>>>>operator i don't know about?
              >>>>>>
              >>>>$name = isset($_GET['name']) ? $_GET['name'] : null;
              >>>>>
              >>>>You should always test with isset() to see if a value passed to your
              >>>>page is set or not. Otherwise you will get a notice if you have them
              >>>>enabled.
              >>>>>
              >>>>
              >>>If setting multiple variables from $_GET, you can also try this:
              >>>>
              >>>$parameters= array('name'=>" value",'example '=>"value1",'an other'=>"value2 ");
              >>>>
              >>>foreach ($parameters as $parameter=>$va lue)
              >>>{
              >>> $$parameter=$_G ET[$parameter]?$_GET['parameter:$val ue;
              >>>}
              >>>>
              >>>
              >>Which is only slightly less dangerous than running with
              >>register_glob als on. Someone can come in and set any variable in
              >>your script by setting it in the query string. And if you miss
              >>initializin g a variable you've got a huge potential security breach.
              >>>
              >>One reason register_global s is no longer enabled by default.
              >>>
              >>
              >Not really. It think it's a clever way to do it. Save you some coding
              >time.
              >>
              >If you see, he only allows the variable in the parameter to be changed.
              >>
              >But then again, it's only my oppinion.
              >>
              >Hendri
              >
              Not at all. I key in
              >
              http://www.example.com?admin=1
              >
              And now in your program $admin=1. And what if $admin is the variable
              which indicates I'm an admin?
              >
              This effectively does exactly what register_global s does - just limits
              it to the $_GET variables.
              >
              Well in order to do that the $parameters variable have to include admin
              is it.

              Hendri

              Comment

              • Toby A Inkster

                #8
                Re: Is there a shortcut i'm missing?

                Lo'oris wrote:
                $name=$_GET['name'];
                if (!$name)
                $name="value";
                >
                i can't figure out how to shorten this thing. Is there some kind of
                operator i don't know about?
                The current PHP method is:

                $name = $_GET['name'] ? $_GET['name'] : 'value';

                It was proposed that PHP 6 should have a new function "ifsetor" which
                would achieve the same thing using this syntax:

                $name = ifsetor($_GET['name'], 'value');

                However, this proposal was rejected in favour for making the middle part
                of the current method optional, so you could write:

                $name = $_GET['name'] ? : 'value';

                However, if you want to use the above, you'll need to wait for PHP 6, and
                your code will not be backwards-compatible with earlier versions of PHP.

                Personally, I've written my own ifsetor() function, and just use that.

                /**
                * Implementation of proposed (but discarded) PHP6 built-in function.
                *
                * @param mixed $value Preferred value.
                * @param mixed $fallback Fallback value.
                * @return mixed isset($value) ? $value : $fallback
                */
                function ifsetor ($value, $fallback)
                {
                if (isset($value))
                return $value;
                return $fallback;
                }

                I use this most often for loading settings. Assuming I have a bunch of
                settings from a file stored in some array:

                $mode = ifsetor($settin gs['mode'], 3); // default mode is 3

                --
                Toby A Inkster BSc (Hons) ARCS
                Contact Me ~ http://tobyinkster.co.uk/contact
                Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

                * = I'm getting there!

                Comment

                Working...