PHP: Notice when error_reporting(E_ALL) - php

When I try to access info that is not presented in xml like so: $someInfo = $element->blabla->cats[0]->src;
PHP shows notice like this: Notice: Trying to get property of non-object
How would I settle the matter?

Just check if the specific element is set befor accessing data:
if(isset($element->blabla->cats[0]->src)){
$someInfo = $element->blabla->cats[0]->src;
}

Related

Notice: Trying to get property of non-object debug error

When the 404 page is triggered, I get a
Notice: Trying to get property of non-object
on the following lines of code when in debug mode
$custom = get_post_custom($post->ID);
and
$parent_id = $parent_id_2 = $post->post_parent;
I know this is suppose to happen because the 404 page triggers because the are no post to show in the first place. To get rid of these notices I need to do a check first to see if there is a post to show. My problem is, exactly how do I do it. Any suggestions?
I would tend to do something like
if (isset($post)){
//action goes here
}
so the action is only performed if post has been set.

uri_to_assoc(n) doesn't work after routing

So this currently loads the page for me.
/controllername/view/id/27/title/shoes
However, I want the user to be able to type in:
/controllername/27/shoes
to view the file. So in the routing file, I made a rule like this,
$route['controllername/(:num)/(:any)'] = "controllername/view/id/$1/title/$2";
The original address works with uri_to_assoc(n) because I have it as
$array = $this->uri->uri_to_assoc(3);
$id = $array['id'];
$title = $array['title'];
But once I route it and use the new address to access the file, I get the errors:
Message: Undefined index: id
Message: Undefined index: title
I do not get these errors with my original url way of accessing it. I guess the value of uri_to_assoc(3) changes once the url is changed but I thought the routing function would take care of that. Can anyone help me get rid of these variable errors?
In this case you'll want ruri_to_assoc:
http://codeigniter.com/user_guide/libraries/uri.html
$this->uri->ruri_to_assoc(n)
This function is identical to [uri_to_assoc], except that it creates
an associative array using the re-routed URI in the event you are
using CodeIgniter's URI Routing feature.
You should still validate your array indices anyways, in case the URL itself is invalid.

PHP variable passing

I'm working with drupal. I have this page something.com/node/13. Inside I have this script
<?php
$door = $_GET["variable"];
echo $door;
?>
When I open the URL like this something.com/node/13?variable=1231 I get the following error:
Error message
Notice: Undefined index: variable in eval() (line 2 of /var/www/html/modules/php/php.module(74) : eval()'d code).
and no output. Any ideas what I might be doing wrong?
The error, partcilarly in drupal 7 simply means that you aren't getting any value for $_GET['variable']. This would be because the url doesn't have a value (someurl?variable=foo). You can avoid the notice by prefixing an # like this:
$door = #$_GET['variable'];
but that won't make the variable return any data. It's pretty unclear from the code you've posted what you're trying to accomplish. You might consider starting there. There is no reason why you can't use $_GET variables in drupal. I do so all the time.
Use drupal_get_query_parameters
Because the rewrite rules with Drupal, you don't grab the $_GET values cleanly directly like that.

How to deal with pass a value twice in a PHP file

I am working on modification of a program. There are two value passes happened in one PHP file, and I get a notification from system like this:
Notice: Undefined index: content in /Users/alexhu/NetBeansProjects/menagerie/svn/trunk/apps/frontend/modules/legacy/legacy_lib/content/utilities/hraprint.php on line 23
And
Notice: Undefined index: isProvena in /Users/alexhu/NetBeansProjects/menagerie/svn/trunk/apps/frontend/modules/legacy/legacy_lib/content/utilities/hraprint.php on line 24
How to avoid it?
That error means you've basically got something like:
$my_arrray = array();
if ($my_array['content'] == ....) {
}
... attempting to access an index/key in an array which has not yet been defined. To guard against it, you'd need something like:
if (isset($my_array['content']) && ($my_array['content'] == ....)) {
Well you are trying to use an index of an array which doesn't exist.
The error is stating what is wrong :-)
You're simply trying to access $array['content'] and $array['isProvena'] in your code. Without the code we can't tell you anything though.

receiving error undefined index on two variables

Using adLDAP.php class
receiving following error: Notice: Undefined index: memberof in /web/ee_web/include/adLDAP.php on line 762
line 762: if (is_array($groups[0]["memberof"])) {
Also receiving error: Notice: Undefined index: count in /web/ee_web/include/adLDAP.php on line 982
line 982: $entries[0]["memberof"]["count"]++;
Unsure of what I need to do to resolve these error messages, it seems that the script is working fine, but I'd like get rid of these errors.
Using: http://adldap.sourceforge.net/wiki/doku.php?id=api
You could edit the code to something along the lines of:
if(isset($groups[0]["memberof"]))
{
if (is_array($groups[0]["memberof"])){ ... }
}
And
if(isset($entries[0]["memberof"]["count"]))
{
$entries[0]["memberof"]["count"]++;
}
It will keep you from getting the errors, though it won't necessarily handle some pretty lax sanity checking by the original author of the code.
It seems like you have your error_level set to show every possible error/warning/notice, that's why you're getting it.
If the script is working fine, then it's not an error, simply a missing check the coder forgot to put in the library.
To get rid of thos messages, you have 2 options:
a) Use # before the calls you do to that library, such as
$var = #the_function(param1);
This will avoid those messages for just that line.
b) Set the error level to something like this with error_reporting():
error_reporting(E_ALL ^ E_NOTICE);
This will affect the whole script you're running.
It's up to you what to use depending on the project.

Categories