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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Noob needs basic help with this relatively simple code

    I'm just trying to get an age range to display properly. This is the array I have and the code. Keep getting syntax errors when I try to load the page. Would anyone be able to help? Cheers

    Array:

    <?php
    $age = (isset($_GET['a']) && $_GET['a'] != 0) ? ($_GET['a'] / 235) : '';
    $gender = (isset($_GET['g'])) ? (($_GET['g'] == 'bhwdd') ? 'Female' : 'Male') : '';
    $state = (isset($_GET['s'])) ? (ucwords(str_replace('xfvcdhs',' ',(substr($_GET['s'],8))))) : '';
    ?>

    Code:

    <?php if( 30 <= $age && $age < 40){
    echo "30-39"; }
    elseif (40 <= $age && $age < < 50){
    echo "40-49";
    }
    elseif ($age > 50){
    echo "50+";
    }
    ?>

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,304
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by jblint View Post
    Keep getting syntax errors when I try to load the page.
    Oh you mean the headers already sent error or the unexpected } error or the oh hang on... you're not wanting to tell us!

    Quote Originally Posted by jblint View Post
    This is the array I have
    Quote Originally Posted by jblint View Post
    Array:

    <?php
    $age = (isset($_GET['a']) && $_GET['a'] != 0) ? ($_GET['a'] / 235) : '';
    $gender = (isset($_GET['g'])) ? (($_GET['g'] == 'bhwdd') ? 'Female' : 'Male') : '';
    $state = (isset($_GET['s'])) ? (ucwords(str_replace('xfvcdhs',' ',(substr($_GET['s'],8))))) : '';
    ?>
    Thats not an array at all. An array is a list / collection of items. You're using a couple of those items FROM the $_GET array and setting a variable with them.

    Quote Originally Posted by jblint View Post
    Code:

    <?php if( 30 <= $age && $age < 40){
    echo "30-39"; }
    elseif (40 <= $age && $age < < 50){
    echo "40-49";
    }
    elseif ($age > 50){
    echo "50+";
    }
    ?>
    Quote Originally Posted by jblint View Post
    Would anyone be able to help?
    Yes.. but you need to read the sticky topics if you want help. Just by using the [php] tags it makes our lives so much easier to understand your code. Look at what it does in my signature - it colour codes things and makes it a lot easier for us to understand and thus help you

    Given that you've not given us the error messages and not used the tags you might not get many interested helpers assisting you

    For a start, this line is wrong:
    $age = (isset($_GET['a']) && $_GET['a'] != 0)

    Should be:
    $age = ((isset($_GET['a'])) && ($_GET['a'] != 0))

    This line:
    (ucwords(str_replace('xfvcdhs',' ',(substr($_GET['s'],8))))) : '';

    You don't need substr() in its own set of parens like that.

    Your if and elseif you are testing two things that should be in seperate parens in one set. In php you can't do that so this:
    if( 30 <= $age && $age < 40){

    Should be:
    if ((30 <= $age) && ($age < 40)){

    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Oh you mean the headers already sent error or the unexpected } error or the oh hang on... you're not wanting to tell us!
    Ha no I just didn't know what to tell you guys. The error code is simply this:

    Parse error: syntax error, unexpected '<' in /home/presence/public_html/maturedatingonly.us/fr/index.php on line 50

    Quote Originally Posted by tangoforce View Post
    Yes.. but you need to read the sticky topics if you want help. Just by using the [php] tags it makes our lives so much easier to understand your code. Look at what it does in my signature - it colour codes things and makes it a lot easier for us to understand and thus help you
    Those stickies...

    PHP Code:

    <?php
    $age 
    = (isset($_GET['a']) && $_GET['a'] != 0) ? ($_GET['a'] / 235) : '';
    $gender = (isset($_GET['g'])) ? (($_GET['g'] == 'bhwdd') ? 'Female' 'Male') : '';
    $state = (isset($_GET['s'])) ? (ucwords(str_replace('xfvcdhs',' ',substr($_GET['s'],8)))) : '';
    ?>
    Do I still include the last part for the first line? ie.
    PHP Code:
    ? ($_GET['a'] / 235) : ''
    Line 50, where it says there is an error, is the 1st one that begins with elseif:

    PHP Code:
    <?php 

     
    if((30 <= $age) && ($age 40)){
     echo 
    "30-39"
    }
    elseif ((
    40 <= $age && $age < < 50)){ 
     echo 
    "40-49"
    }
    elseif (
    $age 50){ 
     echo 
    "50+"
    }

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    $age < < 50)

    < < Isn't an operator

    PHP Code:
     <?php 

     
    if((30 <= $age) && ($age 40)){
     echo 
    "30-39"
    }
    elseif((
    40 <= $age) && ($age 50)){ 
     echo 
    "40-49"
    }
    elseif (
    $age >= 50){ 
     echo 
    "50+"
    }
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #5
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    $age < < 50)

    < < Isn't an operator
    Thank you. That did the trick!!!

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    also on your original code you had nothing for if the age was 50

    i added an equals to the last elseif

    PHP Code:
    elseif ($age >= 50){ 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,304
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by jblint View Post
    PHP Code:
    elseif ((40 <= $age && $age < < 50)){ 
    Quote Originally Posted by Arcticwarrio View Post
    PHP Code:
    elseif((40 <= $age) && ($age 50)){ 
    Notice what Arctic did there? I told you about this before!
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Too much work.
    First, I want to point out that whilst < < is not an operator, << is an operator. It wouldn't help you here at all though, if you attempt to left shift over 32 in PHP, you'll end up with unusual results since PHP only interprets numbers to 32 bits. I believe this applies to any precompiled version of PHP, although if you compile it yourself for x64, you *may* be able to go larger (I can't be sure on that, but last I checked the union for zval was using a long).
    Next:
    You don't actually need as many brackets as you may think with using ternaries. The problem simply comes to how the ternary acts in the current context it is working in. print isset($_GET['a']) && $_GET['a'] != 0 ? $_GET['a'] / 235 : 0.0; would work.
    It will also work with an assignment, so this is actually valid:
    PHP Code:
    $age = isset($a['a']) && $a['a'] != $a['a'] / 235 0.0// use 0.0 as your default as you are working with a float as end result. 
    However, do note that it is actually a VERY good idea to use brackets around these in PHP, in particular the ENTIRE ternary. The reason why is that in PHP ternary operations use left to right assignment of operators, while pretty much every other language uses right to left precedence for ternary. This becomes apparent if you move a ternary into using nested ternaries, or concatenated into a string. A quick example of that:
    PHP Code:
    // In C this is how it should work:
    int main(int argcchar **argv)
    {
        
    char *"cat";
        
    // below will print "It's a CAT!"
        
    printf("It's a %s!"
            
    strcmp(a"cat") == "CAT" :
            
    strcmp(a"dog") == "DOG" :
            
    strcmp(a"mouse") == "MOUSE" :
                
    "UNKNOWN???");
        return 
    0;


    And in PHP:
    PHP Code:
    <?php
    $a 
    "cat";
    // below will print "It's a MOUSE!"
    printf("It's a %s!"
        
    strcmp($a"cat") == "CAT" :
        
    strcmp($a"dog") == "DOG" :
        
    strcmp($a"mouse") == "MOUSE" :
            
    "UNKNOWN???");
    So clearly that's not correct.

    Down to the ifs. You don't need to create ranges when you are working with them. Simply blanket them with less than or greater than and work in the proper order. So for counting up, you'd blanket using <. It doesn't *quite* fit into what you have here since there is no case for < 30, but I'll make one up.
    PHP Code:
    if ($age 30)
    {
        print(
    '<30');
    }
    else if (
    $age 40)
    {
        print(
    '30 - 39');
    }
    else if (
    $age 50)
    {
        print(
    '40 - 49');
    }
    else
    {
        print(
    '50+');

    Now since you are working by ignoring the < 30 range, it may be wiser to compare backwards and work down from 50+. That way you don't need to use an else to blanket the < 30.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    slightly off topic but have you ever looked at the switch() function?

    PHP Code:

    <?php
        
    foreach($_GET as $k => $v){
        switch(
    $k){
            case 
    'a':
                
    $age $v/235;
                if(
    $age >= 30){if($age <= 40){$agegroup='30-39';}if($age <= 50){$agegroup='40-49';}if($age 50){$agegroup='50+';}}
                break;
            case 
    'g':
                
    $gender = (($v == 'bhwdd') ? 'Female' 'Male');
                break;
            case 
    's':
                
    $state ucwords(str_replace('xfvcdhs',' ',(substr($v,8))));
                break;
        }

    }
    echo 
    $agegroup;
    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

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