PHP parse error when use [] - php

The code is
// Get singleton (first value from row with single value)
static function singleton($arg, $params = false) {
return pg_fetch_row(SQL($arg, $params))[0];
}
The error message is
2014-02-19 12:54:23: (mod_fastcgi.c.2701) FastCGI-stderr: PHP message: PHP Parse error: syntax error, unexpected '[' in /var/www/blockexplorer.com/htdocs/includes/sql.inc on line 69
I think there is a config that can fix it.

This depends on PHP version you are using. If you are using PHP 5.4 or above then your code will not give error otherwise you will have to store the result in a variable and use it.
Reference : PHP 5.4
Look for "Array Dereferencing" here.

Put the result of the function in a variable
static function singleton($arg, $params = false) {
$foo = pg_fetch_row(SQL($arg, $params));
return $foo[0];
}

PHP does not support anonymous arrays. Use an named array instead:
static function singleton($arg, $params = false) {
$row=pg_fetch_row(SQL($arg, $params));
return $row[0];
}

Related

Call to a member function prepare() on boolean

I booted up wamp today to check something for one of my website and ran across a php error
[30-Jan-2022 16:57:28 UTC] PHP Fatal error: Uncaught Error: Call to a member function prepare() on boolean in C:\wamp64\www\includes\class.database.php:130
Stack trace:
When checking the php file in question i have no idea how to fix this, anyone that can help?
// Allow for prepared arguments. Example:
// query("SELECT * FROM table WHERE id = :id", array('id' => $some_val));
$sth = $this->db->prepare($sql);
$debugSql = $sql;
$params = array();
if (is_array($args_to_prepare))
{
foreach ($args_to_prepare AS $name => $val)
{
$params[':' . $name] = $val;
$debugSql = preg_replace('/:'.$name.'/',"'".$val."'", $debugSql);
}
}
The best way to reuse functions is to put it inside of the include file, then include it at the top of each file you'll need it. So inside of your db_connection.php, create your function:
Click this

php make set_error_handler return an error string

I need to put a contents of 'PHP Warning' into a variable. For these purposes I got the following code (set_error_handler() in a php class):
$data = '';
set_error_handler( function() use ($this, &$data) { $data = $this->my_error_handler(); return $data; } );
//some code that throws Warning
restore_error_handler();
function my_error_handler($errno, $errstr) {
return $errstr;
}
But I get the following 500 error: Type error: Too few arguments to function my_error_handler(), 0 passed.
If I replace a callable above with a string, code works without a 500 error, but I cannot pass a variable in it and return it with a contents of warning thrown. Any ideas how to fix it? Thank you.

PHP how to have declare a class member?

Slightly confused with PHP because it does not declare object variable types.
This is simplified code which does not work. I get why I get an error but not sure how in PHP I can specify that $pb is a PushBot object and so it has methods which can be used.
class Bot
{
public $pb;
//Constructor
function __construct(){
require_once('../PushBots.class.php');
// Application ID
$appID = '';
// Application Secret
$appSecret = '';
// Push The notification with parameters
$this ->pb = new PushBots();
$this ->pb->App($appID, $appSecret);
}
//Method. The $this->pb->Push() does not work
public function sendMessage(){
$this->pb->Push();
}
}
//Client calling the class
$bot = new Bot();
$bot->sendMessage();
The error I get is :
Parse error: syntax error, unexpected '$' for when the line
$this->pb->Push();
is called.
I guess its because it does not know that $pb is a PushBot object at this stage ?
Can I not declare it something like :
Public Pushbot $pb;
Parse error: syntax error, unexpected '$' for when the line
This is a parse error, meaning your code has not even been run yet. Check your code for any sytnax errors (the code you posted does not contain the syntax error).
how in PHP I can specify that $pb is a PushBot object
Although unrelated to the syntax error, if you were using some sort of dependency inversion you could use type-hinting to require a certain object to be passed:
// will cause fatal error if anything other than an object of type Pushbot is passed
function __construct(Pushbot $pb)

Fatal error when trying to format output using date_diff()

I'm using PHP 5.3.6 and when I try to run the code bellow I get the following error: "
Fatal error: Call to a member function format() on a non-object in ...".
function diferenta_date($data_inceput, $data_sfarsit){
$interval = date_diff(date_create($data_inceput), date_create($data_sfarsit));
$output = $interval->format("Years:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i,Seconds:%s");
$return_output = array();
array_walk(explode(',', $output), function($val, $key) use(&$return_output) {
$v = explode(':', $val);
$return_output[$v[0]] = $v[1];
});
return $return_output;
}
What's wrong?
You need to check the return values. The documentation says date_diff() returns:
The DateInterval object representing the difference between the two dates or FALSE on failure.
date_diff() is failing and you are trying to use FALSE as an object.

PHP object property declaration error

I'm having a bit of trouble defining a property in a PHP class I'm creating.
<?php
class news_parser {
public $var1 = Array();
function contents($parser, $data) {
printf($data);
}
function start_tag($parser, $data, $attribs) {
printf($data);
}
function end_tag($parser, $data) {
printf($data);
}
function parse() {
if(!$file = fopen("http://services.digg.com/2.0/story.getTopNews?type=rss&topic=technology", "r"))
die("Error opening file");
$data = fread($file, 80000);
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, array($this, "start_tag"), array($this, "end_tag"));
xml_set_character_data_handler($xml_parser, array($this, "contents"));
if(!xml_parse($xml_parser, $data, feof($fh)))
die("Error on line " . xml_get_current_line_number($xml_parser));
xml_parser_free($xml_parser);
fclose($fh);
}
}
$digg_parser = new news_parser();
$digg_parser->parse();
echo phpversion();
?>
Produces the following error:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /homepages/8/d335242830/htdocs/caseyflynn/php/display_formatted_RSS_feed.php on line 3
As far as I can tell I have the correct syntax. My server is running PHP 4.5. Any ideas?
My server is running PHP 4.5
This is your problem: PHP 4 doesn't know the public keyword - along with a heap of other OOP features.
As #konforce says, in the code you show, you can simply switch to using the var keyword. But the best thing would really be to switch to PHP 5. PHP 4's time is really, really over.

Categories