Accessing arrays through methods [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 8 years ago.
Improve this question
Why is it that in PHP 5.3.8:
$obj->foo()['bar']; // syntax error
$obj->foo()->['bar']; // valid
But in PHP 5.4.9 it's reverse:
$obj->foo()['bar']; // valid
$obj->foo()->['bar'] // syntax error

$obj->foo()['bar']; is valid in PHP >= 5.4.0
$obj->foo()->['bar'] is not valid in any version even if you add the required ;
So in versions before 5.4.0 you will get a parse error on the first line that will stop execution and not show the parse error on the next line.
In versions 5.4.0 and greater the first line works but you get a parse error on the second. So if you reverse them, you will always get the parse error for $obj->foo()->['bar'] in any version.

Example $obj->foo()->['bar'] was never valid, in any PHP version; see here.
Maybe you got it mistaken with syntax like $obj->foo()->{'bar'}; see here.
Example $obj->foo()['bar'] is valid from PHP >= 5.4.0; see here.

Support for
$obj->foo()['bar'];
came in PHP 5.4.0 as support for function array dereferencing was added only from that version.
This useful page explains it:
https://wiki.php.net/rfc/functionarraydereferencing

Your first example has never been valid.
$obj->foo()->['bar']; // is invalid in any PHP implementation
However with method chaining support: if $obj->foo() returns an object with a bar property this will work:
$obj->foo()->bar;

Related

PHP throwing "Use of Undefined Constant" Despite Quotes in String [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I have a PHP file wherein I'm trying to set a cookie; here is the code:
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$coname = ‘logged’;
$coval = ‘false’;
setcookie($coname,$coval);
?>
Logged is the name of the cookie, false is the value. Right off the bat it's throwing:
Notice: Use of undefined constant ‘logged’ - assumed '‘logged’' in (path) on line 4
Notice: Use of undefined constant ‘false’ - assumed '‘false’' in (path) on line 5
It appears to be reading these strings as constants, then. Every resource I can find recommends solving this by enclosing the string in quotes, which I've tried with both single and double quotes to no avail. If anyone knows why the error persists, it would be a huge help. Thanks!
You're using back- and forward-ticks instead of single quotation marks, so PHP is trying to interpret ‘logged’ and ‘false’ as constants, which aren't defined.
Try this instead:
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$coname = 'logged';
$coval = 'false';
setcookie($coname,$coval);
?>

Function bind_param() on a non-object | PHP MySQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I edited my code with prepared statments(I didn't used them before). I get an error
"Call to a member function bind_param() on a non-object". I googled that error and I found cause - error is caused if query has syntax error. I'm looking last 10 minutes in query and I can't find syntax error. Can somebody help me? Thanks!
// QUERY BEFORE
$_hsync_statment->bind_param("sisssss", $_hsync_ime, $_hsync_id, $_hsync_nista, $_hsync_nista, $_hsync_mail, $_hsync_datum, $_hsync_vrijeme);
if(!$_hsync_statment->execute()) $_hsync_reg_status = -1;
// POVEČAVA BROJ REGISTRIRANIH RAČUNA
$_hsync_statment = $_hsync_konekcija->prepare("UPDATE $_hsync_srv SET Clanova = ?");
$_hsync_statment->bind_param("i", $_hsync_id + 1); // THIS LINE
if(!$_hsync_statment->execute()) $_hsync_reg_status = -1;
I tried to close every statment after it gets executed. That doesn't help.
So what's wrong with
$_hsync_statment->bind_param("i", $_hsync_id + 1); // THIS LINE
The fact that $_hsync_id is a variable that holds an int. when you add 1 to int. It produces an int that's not acceptable to bind_param. bind_param expects an object. Try this:
$_hsplus = $_hsync_id + 1;
$_hsync_statment->bind_param("i", $_hsplus); // THIS LINE
So now why did I get two downvotes when the manual clealy says:
Note that mysqli_stmt_bind_param() requires parameters to be passed by
reference, whereas
The error message Call to a member function bind_param() on a non-object... means that you haven't properly instantiated the object $_hsync_statment before calling bind_params() on it.
have intiated the db connection to the $_hsync_statment
$_hsync_statment = $db->stmt_init();

function split() deprecated - besides preg_split what else needs to be changed in this code? general idea [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 know that there are several questions about this issue, but this is a particullar case...
In the following code (in the the first and the last line) I have to replace split with preg_split, but I think something else needs to be changed too.
Please tell me what should I change in the code for it to work, and the theory behind this change, i.e. the gereral idea behind switching between split and preg_split. The code which needs the transition is:
$opt = split("_",$key);
if($opt[0]=="id" && $val!="0" && $val!=""){
some queries
$shuffle=split("_",$_POST['all_'.$i]);
Use explode instead of split. Your code should look like this :
$opt = explode("_",$key);
if($opt[0]=="id" && $val!="0" && $val!=""){
some queries
$shuffle=explode("_",$_POST['all_'.$i]);
Documentation : http://fr2.php.net/explode
PHP is in the process of dropping an older POSIX-compatible regex extension in favour of the newer PCRE extension. This means that older functions like split() and ereg() will be removed in time.
The PCRE equivalent for split() is preg_split(), which has a modified syntax. For your code you'd use:
$opt = preg_split("/_/",$key);
However, a Regex function is a heavyweight tool and isn't required here. You just need explode(), like this:
$opt = explode("_",$key);

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.

Parse error: syntax error in PHP? [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
Hi guys I'm following this tutorial and I'm getting an extremely strange error message in my PHP when I try and run it in the web browser. The code is as follows:
<?php
// Pull in the NuSOAP
require_once('nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
//(MyService is name of our service)
$server----->configureWSDL('MyService', 'urn:MyService');
// Character encoding
$server->soap_defencoding = 'utf-8';
//-------------------------------------------------
//Registrations of our functions
//-------------------------------------------------
//Our web service functions will be here.
//-------------------------------------------------
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
Which is exactly as it's written in the tutorial, yet I keep getting this error message every time I run the PHP file:
UPDATE now I'm getting this error
Parse error: syntax error, unexpected T_DEC in
/home/a1335235/public_html/MyService.php on line 8
Can anyone figure out why?
As Mark Baker said in the comments (and I'll delete this if he posts first), your code is:
code require_once();
First and foremost code is not PHP. This should be require_once and you can see this in the PHP Manual. Secondly, require_once is a language construct. It's not a function call, so you don't need the (). You should have:
require_once 'nusoap.php';
The syntax error is telling you exactly what line the problem is on, so read it and google that bit of code to see how others are doing it and where you are going wrong in the future.
Now your issue is on line 8:
$server----->configureWSDL
This is not valid PHP either, unless you've heavily modifed the source code of the language, which you haven't. Change this to:
$server->configureWSDL()
That's how you call methods on objects. You should be reading the manual on Objects to see how this works.

Categories