SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Isset a variable which name of the left 3 characters is 'var'

    Code:
    $var='abc';
    [-3-]if(isset($var)){
    [-4-]echo 'yes';} else {
    [-5-]echo 'no';}
    The code above produces 'yes'.
    If there is no $var before the if-clause, it will produces "no".

    Let's suppose I have a variable named 'var??'.
    I know only 3 left letters of the variable name.
    I like to know the variable is set or not.

    The would-be-code below does not work correctly, but I hope it shows what I want.
    Code:
    woulde-be-code
    $var1='abc';
    if(isset($left(varName,3)='var')){
    echo 'yes';} else {
    ]echo 'no';}
    How can I check whether a variable which I know only 3 left letters of the variable name
    is set or not?

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Instead of trying to patch together variable names, it's better to use data structures such as arrays.

    Code PHP:
    $vars = [
        'abc',
    ];
     
    if (!empty($vars)) {
        echo 'yes';
    } else {
        echo 'no';
    }
     
    // yes

    Code PHP:
    $vars = [
    ];
     
    if (!empty($vars)) {
        echo 'yes';
    } else {
        echo 'no';
    }
     
    // no
    "First make it work. Then make it better."


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
  •