The following code
<?php
$email_domain = "abc#gmail.com";
$email_user = "Roshan";
$email_pass = "admin";
$email_quota = "200";
$call = array(domain=>$email_domain, email=>$email_user, password=>$email_pass, quota=>$email_quota);
echo json_encode($call);
?>
generated following error:
Notice: Use of undefined constant domain - assumed 'domain' in C:\xampp\htdocs\test2.php on line 7
Notice: Use of undefined constant email - assumed 'email' in C:\xampp\htdocs\test2.php on line 7
Notice: Use of undefined constant password - assumed 'password' in C:\xampp\htdocs\test2.php on line 7
Notice: Use of undefined constant quota - assumed 'quota' in C:\xampp\htdocs\test2.php on line 7
{"domain":"abc#gmail.com","email":"Roshan","password":"admin","quota":"200"}
I want to know the occurrence of this and in this case what can be done to prevent this error.
array(name => value) is not a valid syntax, php doesn't now what name is. You need quotation marks around your array keys.
Use array("domain" => $email_domain, ... etc.
You should use quotes around the keys in the array, like this :-
$call = array('domain'=>$email_domain, 'email'=>$email_user, 'password'=>$email_pass, 'quota'=>$email_quota);
If you want to use literals as your array keys, you need to surround them in either double or single quotes:
$call = array('domain'=>$email_domain,
'email'=>$email_user,
'password'=>$email_pass,
'quota'=>$email_quota);
Related
This works with simple variables. But it shows empty result with complex variables. AM I MISSING SOMETHING HERE? or is there anyother way around. Thanks.
#1. This works with simple variables.
$object = "fruit";
$fruit = "banana";
echo $$object; // <------------ WORKS :outputs "banana".
echo "\n";
echo ${"fruit"}; // <------------ This outputs "banana".
#2. With complex structure it doesn't. am I missing something here?
echo "\n";
$result = array("node"=> (object)array("id"=>10, "home"=>"earth", ), "count"=>10, "and_so_on"=>true, );
#var_dump($result);
$path = "result['node']->id";
echo "\n";
echo $$path; // <---------- This outputs to blank. Should output "10".
Not exactly using variable variables, but if you want to use a variable as the var name, eval should work
$path = "result['node']->id";
eval("echo $".$path.";");
From php.net's page on variable variables
A variable variable takes the value of a variable and treats that as the name of a variable.
The issue is that result['node']->id is not a variable. result is the variable. If you turn on error reporting for PHP notices you will see the following in your output:
PHP Notice: Undefined variable: result['node']->id ...
This can be solved as follows:
$path = "result";
echo "\n";
echo ${$path}['node']->id;
The curly braces around $path are required.
In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you write $$a[1] then the parser needs
to know if you meant to use $a[1] as a variable, or if you wanted $$a
as the variable and then the [1] index from that variable. The syntax
for resolving this ambiguity is: ${$a[1]} for the first case and
${$a}[1] for the second.
If not present the statement is equivalent to
${$path['node']->id}
which will result in the following output:
PHP Warning: Illegal string offset 'node' in /var/www/html/variable.php on line 18
PHP Notice: Undefined variable: r in /var/www/html/variable.php on line 18
PHP Notice: Trying to get property of non-object in /var/www/html/variable.php on line 18
This worked fine for me on my old server but I have moved to a new server with a more recent version of php and I am getting the following error
Notice: Use of undefined constant company_name - assumed 'company_name' in
How do I define the result as a constant in code below?
$q1 = mysql_query("SELECT company_name FROM company");
print "<ul class='mktree'>"; //open list1
while($nt=mysql_fetch_array($q1)){
print "<li><span>Company - $nt[company_name]</span></li>";
$comp = $nt[company_name];
You should quote your array keys.So
$nt[company_name] must be $nt['company_name']
When you use it without quotes it will look for constants called company_name. When it doesn't find such a constant, PHP (bizarrely) interprets it as a string ('company_name'). Obviously, this can easily break if you do defined such a constant later (though it's bad style to have lower-case constants).
You have to quote the index name.
$nt['company_name']
or company_name will be a undefined constant, and php will assume the value is 'company_name', then trigger a Notice.
You missed out quotes in print $nt[company_name] and $comp = $nt[company_name];
Try with
print "<li><span>Company - $nt['company_name']</span></li>";
$comp = $nt[company_name];
Also if you want to echo the value of $nt[company_name], you have to append the value like:
print "<li><span>Company - ".$nt['company_name']."</span></li>";
I am trying to install a arcade script and keep getting these errors, can anyone help out?
Notice: Undefined index: do in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 34
Notice: Use of undefined constant sql - assumed 'sql' in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 34
Notice: Undefined index: do in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 37
Notice: Use of undefined constant sql2 - assumed 'sql2' in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 37
This is line 34 to 38
<? if ($_GET['do'] == sql) {
include ('sql1.php');
}
else if ($_GET['do'] == sql2) {
include ('sql2.php');
I have tried if isset($_GET but I might have done it wrong as of the closed bracket { for next line, I was told to add another bracket as well but never told where.
Thanks for your help :)
Try
if (isset($_GET['do']) && $_GET['do'] == 'sql')
sql by itself is nothing to PHP. You need to have it as a string or variable, as follows:
<? if ($_GET['do'] == "sql") {
or
$var = "sql";
<? if ($_GET['do'] == $var) {
As for the line 37 error, the server cannot find the GET variable named 'do', so you need to find out why it's not being set. You might consider adding this to earlier parts of your script:
if (!isset($_GET['do'])) {
exit("do variable was not set!");
}
else {
$do = $_GET['do'];
}
That way, you don't have to keep typing $_GET['do'], which can get tedious.
I experience something strange with an undefined index..
$vatcode = 'U25';
echo $this->vatcode_ids['U25']."\n";
echo $this->vatcode_ids[$vatcode]."\n";
foreach($this->vatcode_account_ids as $id => $vatcode){
echo $vatcode."\n";
echo $this->vatcode_ids[$vatcode]; // undefined index
}
this returns:
681
681
U25
Notice: Undefined index: U25 in /var/www/.....php on line 64
I don't get it?!
From empty line printed before Notice massage i assume your $vatcode variable contains some ending new line character. If so it does not match any key in $this->vatcode_ids array.
You should use some trimming function as Dan Lee suggested in comments.
While populating an array with data from a SimpleXML call, PHP throws exception to what it believes as 'Undefined' keys, however, the output is actually correct.
$doc = new SimpleXmlElement($http_result, LIBXML_NOCDATA);
$result = array();
$x = 0;
foreach($doc->users->user as $item) {
$result['user'][$x]['id'] .= $item->id;
$result['user'][$x]['name'] .= $item->name;
$result['user'][$x]['email'] .= $item->email;
$x++;
}
print json_encode($result);
This actually outputs what I expect, i.e. {"user":[{"id":"4843977","name":"Test New User","email":"test#newuser.com"}]}
However, the following errors are also present, and I'm not totally sure why - this doesn't appear in 5.2.6 but does for 5.2.10
Notice: Undefined index: user in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined offset: 0 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
Notice: Undefined offset: 1 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
Notice: Undefined offset: 2 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
I think You must change just ".=" to "=" in lines:
$result['user'][$x]['id'] = $item->id;
$result['user'][$x]['name'] = $item->name;
$result['user'][$x]['email'] = $item->email;
You don't define what are $result['user'] and $result['user'][$x].
You need to instantiate them as array so you won't get that error.
$result['user'] = array();
foreach($doc->users->user as $item) {
$result['user'][$x] = array();
$x++;
}
For the undefined indexes in the fields, the problem is similar. You use ".=" when the variable doesn't exists yet. So you should instantiate it first with an empty string.
$result['user'][$x]['name'] = '';
You need to initialize the $result array first:
$result = array('user' => array());
And since you’re using the string concatenation and assignment operator .=, that would also apply to the $result['user'][$x] arrays:
foreach($doc->users->user as $item) {
$result['user'][$x] = array(
'id' => null,
'name' => null,
'email' => null
);
$result['user'][$x]['id'] .= $item->id;
$result['user'][$x]['name'] .= $item->name;
$result['user'][$x]['email'] .= $item->email;
$x++;
}
But that’s not necessary since you can also write it like this:
$result = array('user' => array());
foreach($doc->users->user as $item) {
$result['user'][] = array(
'id' => $item->id,
'name' => $item->name,
'email' => $item->email
);
}
Edit Since we elaborated that the attributes of $item are SimpleXMLElement objects too, $item->attr[0] is required to address the string value itself. Thus:
$result = array('user' => array());
foreach($doc->users->user as $item) {
$result['user'][] = array(
'id' => $item->id[0],
'name' => $item->name[0],
'email' => $item->email[0]
);
}
It happens, because you're not just setting the array values, but you're concatenating to the current value:
$result['user'][$x]['id'] .= $item->id;
This line means "take the current value of $result['user'][$x]['id'] and add $item->id to it". The notice is then thrown, because the current value is not yet existent.
Amend the code to this
$result['user'][$x]['id'] = $item->id;
and you should be safe. No idea though, why 5.2.6 is not throwing the errors, maybe you should check with the error_reporting setting in the php.ini.