Parse error when using variable variables [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Here is my code inside of a function:
list(${$page}Records, ${$page}MetaData) = getRecords(array(
'tableName' => $page,
'where' => '', // load first record
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
Problem is, the very first line of it throws this error :
Parse error: syntax error, unexpected T_STRING, expecting ',' or ')' in /[edited]/includes/functions.php on line 10
I've tried a bunch of different ways of going about this but I don't know much about PHP. Anyone know what's going on here?

${$page}Records
This is not how you use variable variables. PHP has no idea what you mean with Records there.
Try this:
${$page.'Records'}
PHP will run the code inside the {} and use that string as a variable name.

Related

PDO parse error unexpected ":" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to establish a new PDO instance with the following code
<?php
$handler = new PDO(“mysql:host=127.0.0.1;dbname=database”,
“root”, “”);
I get the following error message:
Parse error: syntax error, unexpected ':', expecting ',' or ')' in /Applications/XAMPP/xamppfiles/htdocs/example/connection.php on line 2
There has been no problems like this before until I attempted to do this on a new Mac. Can someone help with this issue?
try this:
$handler = new PDO("mysql:host=127.0.0.1;dbname=database",
"root", "");
your quotes were wrong
You should to this:
<?php
$handler = new PDO("mysql:host=127.0.0.1;dbname=database","root", "");

Fatal error: Can't use function return value in write context in [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm getting this error and I can't make head or tail of it.
The exact error message is:
function kdrusha_theme_create_page() {
require_once(get_template_directory().= '/inc/pages/kdrusha-settings.php');
}
add_menu_page("KD Rusha Options", 'KD Rusha', 'manage_options', 'kdrusha-options', 'kdrusha_theme_create_page','',99);
The problem is that you're using .=.
something .= something_else
is shorthand for
something = something . something_else
But your something is a function call, and it generally doesn't make sense to assign to a function call (the exception is when it returns a reference).
You should just use ., which concatenates its parameters and returns the result without assigning it anywhere.
require_once(get_template_directory() . '/inc/pages/kdrusha-settings.php');
You need to put your function return in some variable:
function kdrusha_theme_create_page() {
$template = get_template_directory();
require_once($template.'/inc/pages/kdrusha-settings.php');
}

How to get a value from nested array? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Trying to fetch the first URL field from an array of them that comes from a JSON I have decoded but I get this error:
Parse error: syntax error, unexpected '[' in C:\blabla
foreach($data-> images as $data2) {
print_r(images[0]['url']);
}
I hope its enough of my code to work out what I am doing wrong?
Added: I would like the first "url" and it was getting the last one hence why I am changing the code and trying to debug it here.
Within your foreach you use the variable name you specified in the definition:
So something like...
foreach($data->images as $data2) {
print_r($data2[0]['url']);
}
Although, depending on the structure of the array, I'd imagine that you don't need the number, so it might be:
foreach($data->images as $data2) {
print_r($data2['url']);
}
If you wanted to loop through the values by a number, you'd use a for loop
for ($i = 0; $i <= count($data->images); $i++)
{
print_r($data->images[$i]);
}

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have this bunch of code:
$field_with_lengths = array('menu', 'position', 'visible');
foreach($field_with_lengths as $field_with_lenths => $maxlength){
if(strlen(trim(mysql_prep($_POST[$fieldname]))) => $maxlength){
$errors[] = $fieldname;}
i'm getting the folowing error:
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in C:\xampp\htdocs\vp\edit_info.php on line 22
wich is: if(strlen(trim(mysql_prep($_POST[$fieldname]))) => $maxlength){
i am not managing to find anything wrong on line 22
how can i fix that up?
It's >= (greater than or equal to) not => (equal to or greater than):
if(strlen(trim(mysql_prep($_POST[$fieldname]))) >= $maxlength){
if(strlen(trim(mysql_prep($_POST[$fieldname]))) >= $maxlength){
Equal sign must come after the bracket

How to use ""Whois Class Code"" script? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
<?php
require("whoisClass.php")
$whois=new Whois;
$rs=$whois->whoislookup("99webtools.com"); //Your domain or IP
echo '<pre>'.$rs.'</pre>';
?>
and this is link to show whoisclass.php
http://99webtools.com/php-whois-script.php
The script has an error which you also have (copied and pasted from source), being the missing semi-colon at the end of:
require("whoisClass.php")
^ // <= right there
replace with:
require("whoisClass.php");
and it will run.
Having error reporting on, would have signaled the following error:
Parse error: syntax error, unexpected '$whois' (T_VARIABLE) in /user/home/whois.php on line 3
Always use error reporting:
error_reporting(E_ALL);
ini_set('display_errors', 1);
http://php.net/manual/en/function.error-reporting.php
Footnotes:
When an error is reported on a certain line number, many times the error is on the line before that. In this case it signals being on line 3, yet it's actually on line 2.

Categories