2

I have a piece of code that looks like this:

$result = mysql_query($queryc) or die(mysql_error());   

if(mysql_num_rows($result) > 0)                             
{   
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  {
    echo $row['$field']; 
  }
}

Say that code was in a function and I wanted to pass $field to the $row[''] how would I accomplish that?

In other words I'm attempting to use $row['$field']; and $field is defined elsewhere

1
  • 1
    remove quotes from '$field' $row[$field] Commented Dec 31, 2010 at 18:15

4 Answers 4

4

suppose you have this function definition:

function foo($field) {
    // from that code you have given
    ....
    echo $row[$field];  // no need of the quotation marks around $field
    ....
}
Sign up to request clarification or add additional context in comments.

1 Comment

funny thing is I tried doing it without the quotes but I was looking at the wrong output the entire time. thanks!
4

You'd not put any quotes around $field... like this:

echo $row[$field];

Comments

3

Variables are not expanded in single quotes; they are only expanded in double quotes or in the heredoc syntax:

When a string is specified in double quotes or with heredoc, variables are parsed within it.

So either use double quotes or, even better, just omit them:

$row[$field]

Comments

3

Single quotes inhibit variable substitution.

echo $row["$field"];

or just

echo $row[$field];

The latter is highly recommended as it does not require PHP to parse $row["$field"] into $row[$field]. Saves you some microtime in each iteration.

8 Comments

@thephpdeveloper: Give me 100 developers who prefer the latter and I'll give you 100 who prefer the former. That edit was useless as the OP can choose for himself which solution appeals to him and whether or not micro optimization is needed.
$row["$field"] makes sure your key value is a string. But if you're using $row["$field"], please write is as $row["{$field}"] as a standard. Another example: "My name is {$name} and I'm {$age} years old."`
@Fake Code Monkey Rashid, give me 100 developers who prefer $row["$field"]. I'd seriously doubt you'd find them. This is not really a preference. Without quotes is much better for three reasons: 1. less code to write. 2. less processing 3. less code to understand or possibly misinterpret. Would you ever write $a = "$b";? If not, why would you ever write $row["$field"];?
@Amil, if you're worried about type safety, you should be more explicit before that point anyway. $row["{$field}"] is just completely tortured and over the top.
@Mike Sherov: Did you forget that PHP was like a call to the West. Every end user and his sister packed up a wagon to become a web developer. Given that, do you honestly think I can't find 100 who prefer the former? As for your 3 points, it is a mistake to think that everyone is like you. For some it is preferable to write MORE core to enhance readability even if they lose microseconds in the process. But understand, I did not state that one version was more correct than the other, I merely stated that the OP can choose for himself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.