Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2012
    Posts
    44

    Unanswered: Check if variable or string in array

    Is it possible? If I have this code,

    PHP Code:
    $string "world";
    $array = array("hello"$string); 
    Is it possible to distinguish between the two values in the array? That key 0 is a string written directly, and key 1 is a string stored in a variable?

    Thanks for any help!

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    why would it matter whether it was set as a littoral or as a variable assignment?

    there are functions thjat can tell you what datatype the variable represents. just forget what they are for now. in part if you don't already know what datatype your variable represents then to me your design, not least your variable naming convetion is inadequate.

    there are functions that can check if a value in a varaibel can be coerced to numeric
    Google
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Feb 2012
    Posts
    44
    Quote Originally Posted by healdem View Post
    why would it matter whether it was set as a littoral or as a variable assignment?

    there are functions thjat can tell you what datatype the variable represents. just forget what they are for now. in part if you don't already know what datatype your variable represents then to me your design, not least your variable naming convetion is inadequate.

    there are functions that can check if a value in a varaibel can be coerced to numeric
    Google
    Thanks for replying!

    As my code are right now, I would benefit from distinguish it.

    I have a loop that outputs the result (stats) from a mysql-query. Most of the fields in the db-table are able to output directly, but some needs to be calculated (e.g. a date).

    Before executing this loop, I've created an array like:

    PHP Code:
    $stats = array('First Name'        => 'firstname',
            
    'Last Name'        => 'lastname',
            
    'Team'            => 'teamname',
            
    'height',
            
    'weight',
            
    'Joined Team'        => $joined_team,
            ); 
    The loop could then look like:

    PHP Code:
    foreach ($stats as $nyckel => $verdi){
        (
    is_string($nyckel)) ? $attr $nyckel $attr ucfirst($verdi);
        echo 
    '<dt>' $attr ':</dt>';
        echo 
    '<dd>' $row[$verdi]  . '</dd>';

    So if an attribute doesn't already is in good output format (like height and weight), I don't need a specific attribute name for that one.

    But the problem is with the stat in $joined_team. This is not the same result as in the $row array (from the db-query), but a calculated one.

    That's why it would be great to be able to check if it's from a string or a variable, then I could use:

    PHP Code:
    echo '<dd>' $verdi  '</dd>'
    instead of:

    PHP Code:
        echo '<dd>' $row[$verdi]  . '</dd>'

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    ?
    store a key as well as the value in your associative array and take appropriate action on the key,
    or store an object in the array which contains multiple data
    or have a multi level array

    I don't understand why you'd need to know if it was a littoral or a variable

    functionally
    PHP Code:
    $verdi 'Composer';
    echo 
    '<dd>' $verdi  '</dd>';  
    echo 
    '<dd>$verdi</dd>';  
    echo 
    '<dd>' $row[$verdi]  . '</dd>';  
    echo 
    '<dd>Composer</dd>'
    are the same

    I do so hope you aren't being tempted to use the EAV model. in certain circumstance it has its merits but thats a very very sepcial case.
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Feb 2012
    Posts
    44
    Thanks!

    Let's say there was a function called is_var or something like that (for the above mentioned purpose). Then I would code:


    PHP Code:
    (is_var($verdi)) ? $output $verdi$output $row['verdi']; 
    And then output $output as usual. That's why it would be great!

    But there's isn't any way to know if it was a littoral or a variable right?

    I've never heard of the EAV model, I'll Google it though!

Posting Permissions

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