Reputation: 61
Question: In PHP can I somehow escape the string 'Function' so that it is not recognized as a function?
I am importing data from an SQL database to an SQL database with php. I need to replace the value of a field with an ID. The data is being sent as an array. I have been using the method below to replace IDs that can't automatically be recognized, which has been working. One of the field names in the array, however, is called 'Function'. PHP seems to think I want to write a function and I get an error message
'Parse error: syntax error, unexpected '==' (T_IS_EQUAL), expecting '('
if (isset($Function)) {
if (Function == 'A') {
$functionID = '4';
}
else if (Function == 'B'){
$functionID = '11';
}
}
else {
$functionID = 0;
}
Upvotes: 0
Views: 73
Reputation: 2065
In your code, you are not accessing array data. Instead, you are trying to access a single variable named $Function
.
Suppose the array is stored in a variable named $arr
. Then in order to check value of its 'Function' field (element with index 'Function') you should write:
if ($arr['Function'] == 'A') {
// ...your action code here
}
Upvotes: 1
Reputation: 29975
Yes, you can! The way you were using Function
was wrong (as a bare word without a dollar sign in front it is assumed to either be a reserved word or a constant). Put a dollar sign in front of it so it gets interpreted as a variable.
if (isset($Function)) {
if ($Function == 'A') {
$functionID = '4';
} else if ($Function == 'B'){
$functionID = '11';
}
} else {
$functionID = 0;
}
Please note that Function
is not a string:
"Function"
.Function
is a reserved wordSome_Function
is a constant (as it's not reserved)$Function
is a variableSome_Function()
would have been a function call.Upvotes: 2