Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Posts
    102
    Thanks
    18
    Thanked 0 Times in 0 Posts

    function why isnt this working?

    why isnt this working? when i run the script i get a blank page



    <?php
    function useragent () {
    $useragent = $_SERVER['HTTP_USER_AGENT'];

    echo "$useragent";
    }
    ?>

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    You don't need quotes around $useragent when echoing. Get rid of those quotes and if your page is still blank try running print_r($_SERVER); and see what turns up.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Posts
    102
    Thanks
    18
    Thanked 0 Times in 0 Posts
    thanks, it doesnt work when i remove the quotes either. shouldnt it work?

    how would i put print r in the function?

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    PHP Code:
    <?php 
    function useragent () {
    $useragent $_SERVER['HTTP_USER_AGENT'];

    //echo "$useragent";
    print_r($_SERVER);
    }
    ?>
    That should spit out the entire array of data in $_SERVER. Run that and check to see if 'HTTP_USER_AGENT' is actually set to anything.


    Edit: EDIT: Oh, duh! You haven't called your function yet. You have only declared it. Try this instead:
    PHP Code:
    <?php 
    function useragent () {
    $useragent $_SERVER['HTTP_USER_AGENT'];

    echo 
    $useragent;
    }
    useragent ();
    ?>
    Always try the simplest things first...
    Last edited by Rowsdower!; 03-24-2010 at 01:17 PM.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    You have to call the function for it to run.

    Code:
    <?php 
    function useragent () {
    $useragent = $_SERVER['HTTP_USER_AGENT'];
    
    echo "$useragent";
    }
    
    useragent();
    ?>

    Edit: Bugger, beaten to it.

  • #6
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by MattF View Post
    You have to call the function for it to run.

    Code:
    <?php 
    function useragent () {
    $useragent = $_SERVER['HTTP_USER_AGENT'];
    
    echo "$useragent";
    }
    
    useragent();
    ?>

    Edit: Bugger, beaten to it.
    Yes, but not by far. It's early (here at least) so I'm chalking it up to that!
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Posts
    102
    Thanks
    18
    Thanked 0 Times in 0 Posts
    yeah that probably will help thanks


  •  

    Posting Permissions

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