Error in syntax using php - php

i have this line here.. it gives me an error..
Could you please take a look at this?
Thanks
$slideshow-auto2=$this->params->get("slideshow-auto2");

Invalid variable name:
$slideshow-auto2=$this->params->get("slideshow-auto2");
^---can't have this in a var name.
You're trying to do (from PHP's view), $slideshow minus constant "auto2" equals ...

I think you're missing a >:
$slideshow->auto2=$this->params->get("slideshow-auto2");
// ^ Right here

$slideshow-auto2 is not a valid variable name. You can't have hyphens in a variable name (PHP sees it as a minus).
Most of the other answers are guessing that you intended to use the -> syntax. If $slideshow is an object and auto2 is a property of that object, then this is what you want.
However, given the context of the rest of your line of code, my guess is that you want to have an actual variable named $slideshow-auto2. Unfortunately, this just isn't allowed. You'll need to work around it. You could name your variable $slideshowAuto2 or $slideshow_auto2 or various other alternatives, but not $slideshow-auto2.

You're trying to substract a property from an object, I guess you want to access that property so add a '>'
$slideshow->auto2=$this->params->get("slideshow-auto2");

Are you trying to use a hyphen within a variable name? That won't work because it's being interpreted as a minus sign and subtracting a property from an object does not work. You probably want something like this instead:
$slideshow->auto2=$this->params->get("slideshow-auto2");
Edit:
If you don't intend to access the property 'auto2', simply replace the hyphen with a valid character for a variable name.

Related

Add values later to a constant-Array

Is there a possibility to add on an existing constant-array some values?
define("USERID", array(123));
I've tried with
define("USERID", '456');
And google didn't get an answer as well,-)
From : http://php.net/manual/en/language.constants.php
A constant is an identifier (name) for a simple value. As the name
suggests, that value cannot change during the execution of the script
You might have tried this:
define('USERID', [123, 456]);
But that won't work, defining a constant a second time is simply ignored by php and keeps the first value it was defined as.
Instead you might consider using a static class variable. Or even a public property or getter method for a class. Or just a global variable.

How do I access the contents of this variable in PHP?

I am using a library, XCRUD, which takes a string argument and is expecting a variable interpolation pattern. Here is how it is used in the documentation, which works fine.
$xcrud->column_pattern('username','My name is {value}');
I want to use this variable as a key to an array, but I can't figure out what syntax is required to access it.
I have tried the following:
$xcrud->column_pattern('PlanNo', $myArray['{value}']);
$xcrud->column_pattern('PlanNo', $myArray[eval('{value}')]);
$xcrud->column_pattern('PlanNo', $myArray[${value}]);
How is it that the function in the library I'm calling can access the variable through {}? Maybe it's unreasonable for me to expect it will exist in the current scope, and it just passes that string somewhere down the line.
Thanks for your help. :)
Try this
$xcrud->column_pattern('PlanNo', $myArray[eval("(" + value + ")")]);

What does PHP operator ->{...} mean?

I recently saw this line in a PHP piece of code:
$dbObject = json_decode($jsonString);
$dbObject->{'mysql-5.4'}[0]->credentials
What does this mean? In the PHP docs we can read, that
Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).
But how can the Object $dbObject be defined to allow ->{...}[...]access? Is this code kind of unsafe? Which PHP version does allow this?
Did I miss anything in the PHP docs?
It's to enable access to properties which would be invalid syntax as bare literals. Meaning:
$dbObject->mysql-5.4[0]->credentials
This is invalid/ambiguous syntax. To make clear to PHP that mysql-5.4 is a property and not a property minus a float, you need to use the {'..'} syntax.
To be exact, ->{..} enables you to use any expression as the property name. For example:
$dbObject->{ sprintf('%s-%.1f', 'mysql', 5.4) }
The curly brace syntax allows you to use a string literal or variable as a property or method name.
This is useful for several reasons:
(per #Deceze's answer): It allows you to access property names that would otherwise not be valid PHP syntax -- eg if the property name contains a dot or dash, per your example. Typically properties like this would be accessed via a __get() magic method, since they would also be invalid syntax to define as an actual property within the class.
(per #feela's answer): It allows you to use variables to reference your properties. This is similar to the $$ variable variable syntax. It's generally not really very good practice, but is useful in some cases.
(not mentioned by any other answers yet): It allows you to remove certain potential ambiguities from your code. Consider the following code for example:
$output = $foo->$bar['baz'];
This is ambiguous. You can resolve the ambiguity by adding braces or brackets:
$output = $foo->{$bar['baz']}; //gets the value of a property from $foo which has a name that is defined in $bar['baz']
$output = ($foo->$bar)['baz']; //gets the ['baz'] element of $foo->$bar array.
This is particularly important because the forthcoming relese of PHP 7 will change the default behaviour of code like like. This will make the language behave more consistently, but will break existing code that doesn't have the braces.
See also the PHP documentation for this change here: https://wiki.php.net/rfc/uniform_variable_syntax
But even without the language change to force the issue, code like this should have braces anyway to help with readability -- if you don't put braces, then you may end up struggling to work out what you were trying to do when you come back to the code in six months' time.
The curly braces syntax in the example is not from the array syntax, but comes from a possible syntax to access variables with variable names.
Curly braces may also be used, to clearly delimit the property name.
They are most useful when accessing values within a property that
contains an array, when the property name is made of mulitple parts,
or when the property name contains characters that are not otherwise
valid (e.g. from json_decode() or SimpleXML).
Examples from the PHP docs:
echo $foo->{$baz[1]} . "\n";
echo $foo->{$start . $end} . "\n";
echo $foo->{$arr[1]} . "\n";
See “PHP Variable variables”: http://php.net/manual/en/language.variables.variable.php
(More examples on the usage there…)
As an addition to #deceze's answer:
The -> operator means that you are accessing an object property. In this case the property name should be mysql-5.4 which is not valid PHP identifier (it must contain letters, numbers or underscores, see here). So it is 100% sure that the property name has been created dynamically.
PHP allows overloading properties using magic method called __get(). The body of this method allows you to handle any property you would wish - this can be any string, or any variable, even an object, which is cast to a string.
So in your case somebody created a class with a __get() magic method that handles string mysql-5.4. The curly braces { and } are given to denote that the string should be treated as property name (however there is no property with this name).
most of the time there is a $ after ->{ so it's like ->$variable
for example we have:
$languageCode = 'en';
$obj->{$languageCode};
$obj->{$languageCode} it is equivalent to $obj->en
============
in your case it's because it's not allowed in php write like:
$dbObject->mysql-5.4
because there is a - and there is a . in the mysql-5.4 and - and . makes a word to two words.
so you have to write it like this:
$dbObject->{'mysql-5.4'}[0]->credentials

How do I access a PHP object attribute having a dollar sign?

I have a PHP Object with an attribute having a dollar ($) sign in it.
How do I access the content of this attribute ?
Example :
echo $object->variable; // Ok
echo $object->variable$WithDollar; // Syntax error :-(
With variable variables:
$myVar = 'variable$WithDollar';
echo $object->$myVar;
With curly brackets:
echo $object->{'variable$WithDollar'};
Thanks to your answers, I just found out how I can do that the way I intended :
echo $object->{'variable$WithDollar'}; // works !
I was pretty sure I tried every combination possible before.
I assume you want to access properties with variable names on the fly. For that, try
echo $object->{"variable".$yourVariable}
You don't.
The dollar sign has a special significance in PHP. Although it is possible to bypass the variable substitution in dereferencing class/object properties you NEVER should be doing this.
Don't try to declare variables with a literal '$'.
If you're having to deal with someoneelse's mess - first fix the code they wrote to remove the dollars then go and chop off their fingers.
C.
There are reflection methods that also allow you to construct method and attribute names that may be built by variables or contain special characters. You can use the ReflectionClass::getProperty ( string $name ) method.
$object->getProperty('variable$WithDollar');

PHP: getting a "use of undefined constant COOKIE_LOGIN" how do I fix this?

I haven't made any changes to the code affecting the COOKIE's and now I get the following:
Use of undefined constant COOKIE_LOGIN - assumed 'COOKIE_LOGIN'
//Destroy Cookie
if (isset($_COOKIE[COOKIE_LOGIN]) && !empty($_COOKIE[COOKIE_LOGIN]))
setcookie(COOKIE_LOGIN,$objUserSerialized,time() - 86400 );
I'm not sure what I need to do to actually change this since I do not know what chnaged to begin with and so cannot track the problem.
Thanks.
You need to surround the array key by quotes:
if (isset($_COOKIE['COOKIE_LOGIN']) && !empty($_COOKIE['COOKIE_LOGIN']))
setcookie('COOKIE_LOGIN',$objUserSerialized,time() - 86400 );
PHP converts unknown literals to strings and throws a warning. Your php.ini probably had the error reporting level to not display warnings but someone may have updated it or something. In either case, it is bad practice to take advantange of PHP's behavior in this case.
For more information, check out the php documentation:
Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not.
This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.
You can't say $_COOKIE[COOKIE_LOGIN] without error unless COOKIE_LOGIN is an actual constant that you have defined, e.g.:
define("COOKIE_LOGIN", 5);
Some people have habits where they will write code like:
$ary[example] = 5;
echo $ary[example];
Assuming that "example" (as a string) will be the array key. PHP has in the past excused this behavior, if you disable error reporting. It's wrong, though. You should be using $_COOKIE["COOKIE_LOGIN"] unless you have explicitly defined COOKIE_LOGIN as a constant.

Categories