SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question what arguments can a function take (quick question)

    Does the first piece of code work, or do I have to use the second one?

    PHP Code:
    //First piece of code
    if (isset($_GET['acct_type']) || $_POST['acct_type'])){
    //do something
    }


    //Second piece of code
    if (isset($_GET['acct_type']) || isset($_POST['acct_type'])){
    //do something

    Thanks!
    Last edited by agentforte; Aug 16, 2007 at 19:26.

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm new to php, but I think you can try elseif for the 2nd argument

  3. #3
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second argument would work how I want it to. I just want to know if the first one would also work (less code)

    the logic is this:

    if either $_GET['acct'] or $_POST['acct'] is set, do something

    the "elseif" is only if you want to do something different depending on the argument. for example:

    if (isset($_GET['acct']) {
    echo 'GET';
    } else if (isset($_POST['name']) {
    echo 'POST';
    }

    I want to do the same thing if either argument is true. I am just not sure if a function can take an OR argument. for example

    does this work: isset($a || $b)
    the same way that this works: isset($a) || isset($b)
    ???

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do it:

    PHP Code:
    $a $_GET['acct'];
    $b $_POST['acct'];

    if (isset(
    $a || $b)) {
    //do something 
    } elseif (isset($a || $b)) {
    //do something


  5. #5
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    wha??

    the elseif statement is useless, if the first one runs, the second one will not. But if the first one doesn't run, the second one will not run either (they are exactly the same). The second statement will never run!

    also there are errors in that code:

    if either the GET or POST variables are not set, you will trigger a "NOTICE, variables undefined" error.

    the point of "isset" is to see if the variable has a value. if not, you don't use it.

    this is how you should use it:
    PHP Code:
    if (isset($_GET['name']) {
    echo 
    $_GET['name'];
    } else {
    echo 
    "GET[name] does not have a value. you cannot echo it's contents";

    I'm glad you are learning php, it's pretty cool when you get the hang of it. What you should really do is get a book that teaches you the proper way to do things. kevin yank's book "how to build database driven websites" is a good one. I bought it a while back and I am doing much better,

    Watch out though, his book does not make you a master and it does not cover many security issues. I did a lot of research online to cover security issues.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (isset($_REQUEST['acct_type'])) { # Contains GET POST COOKIE
        # Do something...
    }

    # Or

    if (isset($_GET['acct_type']) || isset($_POST['acct_type'])) {
        
    # Do Something...

    Yeah...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by agentforte View Post

    I'm glad you are learning php, it's pretty cool when you get the hang of it. What you should really do is get a book that teaches you the proper way to do things. kevin yank's book "how to build database driven websites" is a good one. I bought it a while back and I am doing much better,

    Watch out though, his book does not make you a master and it does not cover many security issues. I did a lot of research online to cover security issues.

    I've already got kevin yanks book...i'm currently reading the Php 5, MySql Bible. I new though. I'm trying to learn the lingo..

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    A function to do all the work yay!
    PHP Code:
    function eisset () {

        
    $args func_get_args();
        
    $args = (func_num_args() == 1) ? explode(' '$args[0]) : $args;
        
    $exp  array_shift($args);

        foreach (
    $args as $arg) {
            
    $e[] = 'isset(' $arg ')';
        }

        switch (
    strtolower($exp)) {

            case 
    'or':
            case 
    '||':
                return 
    'if (' join(' || '$e) . '){return true;}else{return false;}';
            break;

            case 
    'and':
            case 
    '&&':
                return 
    'if (' join(' && '$e) . '){return true;}else{return false;}';
            break;

            case 
    'xor':
                return 
    'if (' join(' xor '$e) . '){return true;}else{return false;}';
            break;

        }

    }

    var_dump(eval(eisset('|| $_GET["someone"] $_POST["someone"]')));
    # Or: var_dump(eval(eisset('||', '$_GET["someone"]', '$_POST["someone"]')));

    // The function returns a string with is then evaluated by eval.
    // if (isset($_GET["someone"]) || isset($_POST["someone"])){return true;}else{return false;} 
    Just cause I felt like not cause its a good idea.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol, yeah a bit overkill for what I need. cool though.

    I don't remember seeing the xor operator before... maybe something similar in C programming a few years back.

    ok I looked it up (instead of being lazy and asking here)

    "if A or B but not both"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •