Results 1 to 9 of 9

Thread: Need help with PHP

  1. #1
    proweb's Avatar
    proweb is offline Joomla Developer
    Join Date
    Jan 2010
    Posts
    217
    Thanks
    4
    Thanked 24 Times in 23 Posts

    Need help with PHP

    I'm stuck with a php code:

    Code:
    if ($currency="EUR") {
        $curr = "€";
    } elseif ($currency="GBP") {
        $curr = "£";
    } else {
        $curr = "$";
    }
    echo $curr;
    The $currency is defined, but $curr is always showing EUR. I'm sure there is only a small thing that I'm missing, but I'm new with php and can't solve this problem.

    Some help would be appreciated!
    Thank you!
    Find Best Web Hosting Service. It's good or bad host? - JustHost Review
    Download free joomla themes for version 1.5 and 1.6

  2. #2
    Naweed is offline Building the net, brick by brick
    Join Date
    Jul 2009
    Location
    India, Earth
    Posts
    26
    Thanks
    69
    Thanked 26 Times in 18 Posts
    Watch out for the "=" assignment operator you have used in the if condition.

    You need to use the comparison operator "==" to check "if a value is equal to some other value."

    For instance,
    PHP Code:
    if($currency "EUR"
    should be
    PHP Code:
    if($currency == "EUR"
    To avoid this error, you can use if("EUR"==$currency). If you happen to miss out an equal sign, the PHP engine will point out the error.

  3. Thanked by:

    Aziz (25 April, 2010)

  4. #3
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,314
    Thanks
    178
    Thanked 309 Times in 236 Posts
    Naweed hit the nail on the head, but I want to add that it's best to avoid these nested if else statements and go for the switch statement such as:

    PHP Code:
    switch ($currency) {
        case 
    'EUR':
            
    $curr '€';
            break;
        case 
    'USD':
            
    $curr '&usd;';
            break;
        default:
            
    $curr '£';
            break;

    Otaku CMS - Import a WordPress blog and manage your site using single-page App technology
    Angular Skills - new site about Front-end App. programming

  5. Thanked by:

    Aziz (25 April, 2010)

  6. #4
    proweb's Avatar
    proweb is offline Joomla Developer
    Join Date
    Jan 2010
    Posts
    217
    Thanks
    4
    Thanked 24 Times in 23 Posts
    Quote Originally Posted by Andy101 View Post
    Naweed hit the nail on the head, but I want to add that it's best to avoid these nested if else statements and go for the switch statement such as:

    PHP Code:
    switch ($currency) {
        case 
    'EUR':
            
    $curr '€';
            break;
        case 
    'USD':
            
    $curr '&usd;';
            break;
        default:
            
    $curr '£';
            break;

    Thank you for your reply and help! This works perfect!
    Find Best Web Hosting Service. It's good or bad host? - JustHost Review
    Download free joomla themes for version 1.5 and 1.6

  7. #5
    stickycarrots's Avatar
    stickycarrots is offline Experienced Net Builder
    Join Date
    Dec 2008
    Location
    QuickInbox.com
    Posts
    753
    Blog Entries
    6
    Thanks
    18
    Thanked 86 Times in 59 Posts
    I still find myself putting one = sign once in awhile and I've been doing this for like 7 years now

  8. #6
    talmadgetab is offline Unknown Net Builder
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    Note : In any programming language when you use If condition, so you must be used == operator. Otherwise, you will get the error.

    Example : if(x==y)

  9. Thanked by:

    Aziz (22 July, 2010)

  10. #7
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,314
    Thanks
    178
    Thanked 309 Times in 236 Posts
    Even more:

    if you want to check if a sequence of characters are in a string, the position may be zero i.e. from the start which can be evaluated as false, so in PHP and I suspect other programming languages you should use 3 equals signs (exactly equal to) such as:

    PHP Code:
    $s "Hello World";

    if (
    strpos($s'Hello') === 0) echo "Found"; else echo "Not found"
    The opposite comparison operator is !==

    PHP Code:
    $s "Hello World";

    if (
    strpos($s'Hello') !== false) echo "Found"; else echo "Not found"
    Otaku CMS - Import a WordPress blog and manage your site using single-page App technology
    Angular Skills - new site about Front-end App. programming

  11. #8
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,314
    Thanks
    178
    Thanked 309 Times in 236 Posts
    Quote Originally Posted by talmadgetab View Post
    Note : In any programming language when you use If condition, so you must be used == operator. Otherwise, you will get the error.

    Example : if(x==y)
    In PHP that is not the case. It's common to use a statement containing a function call with a return value of false or an expected value. Not the most robust way to code, but it is done a lot with database access code.

  12. #9
    jalicia18's Avatar
    jalicia18 is offline Call Center Services
    Join Date
    Dec 2010
    Location
    Philippines
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you use nested if and else, you should put == for equivalent to "is equal to".

Tags for this Thread

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
  •