Results 1 to 9 of 9

Thread: Need help with PHP

  1. 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
    Join Date
    Jul 2009
    Location
    India, Earth
    Posts
    26
    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. #3
    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;


  4. 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

  5. #5
    I still find myself putting one = sign once in awhile and I've been doing this for like 7 years now

  6. #6
    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)

  7. #7
    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"

  8. #8
    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.

  9. #9
    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
  •