-8

Not sure how to name this question but I am looking for feedback to why when I set a variable to some string it will not let me call that variable in a function... For example:

$name = "name";
$quote_name = "'".$name."'";

//echo of $name = name
//echo of $quote_name = 'name'

PHP will not allow me to call:

if($value['name'] == $quote_name){...}

Or

if($value['name'] == '$name'){...}

But it WILL allow me to call (with the whole function):

foreach($question_set_p1 as $key => $value) {
    if($value['name'] == 'name') {
        $first = $key;
        break;
    }
}


$data =  array_filter($question_set_p1, function($arr){ 
    return $arr['name'] == 'name'; 
});

Why does PHP allow me to call the string but not the variable? Is there a way that I can state the variable and not the actual string?

  • 10
    Because `'name'` doesn't equal `name`? – Mike B Oct 01 '14 at 17:53
  • `$quote_name = "'".$name."'";` - why not just do `$quote_name = $name;` and quote it in your SQL, which I'm under the impression you want to use this for. `'".$name."'` is used for that. `... VALUES ('".$name."')` or `... VALUES ('".$quote_name."')` depending on what you want to use as a variable. Don't try and re-invent the wheel, *as it were*. – Funk Forty Niner Oct 01 '14 at 17:53
  • Are you getting an error, or is your `if` not evaluating to `true`. (BTW your `if` won't evaluate to `true`....) – Crackertastic Oct 01 '14 at 17:53
  • *"Unclear what you're asking"* - is leading by a nose. – Funk Forty Niner Oct 01 '14 at 18:01
  • What is setting `$value['name']`? – Jay Blanchard Oct 01 '14 at 18:01
  • If you want to know why this is an issue, you should call `var_dump()` on each of your variables. It will show you the type of variable, in many cases the length of the variable and what is the actual value of the variable. The function is used for debugging. You can visually compare the variables and see why they are different. – Jonathan Kuhn Oct 01 '14 at 18:08
  • Have done that and they are identical. – stack overflow Oct 01 '14 at 18:09
  • I think I see part of the issue here. If the variable is defined outside of a function, the variable is not available inside the function. This is because of function scope. There are several answers covering this topic. It's not like javascript where global variables are available pretty much everywhere. You need to explicitly call `global $varName` inside of the function in order to make it available to that one function. [Reference](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Jonathan Kuhn Oct 01 '14 at 18:12
  • variable is not outside the function – stack overflow Oct 01 '14 at 18:27

2 Answers2

2

Remove quote in second var, like this:

$name = "name";
$quote_name = $name;

if($value['name'] == $quote_name){...}
Julien
  • 1,946
  • 3
  • 33
  • 51
1

suppose

$value['name'] = 'abcd';
$name = 'abcd';
$quot_name = " ' ".$name." ' ";//(as you entered).

Now if you echo $quot_name it will return 'abcd' (with quotes) and if you echo $value['name'] it will return abcd (without quotes).

Therefore $quot_name should be $name (without quotes) or you should add quotes to $value['name'] too.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119