problem in undifined varible

#1
[eluser]Amol Kharat[/eluser]
Hello all,

I am new in codeigniter and i have problem:

-----------------------------------------
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: abc

Filename: views/p_dashboard.php

Line Number: 19
----------------------------------------

This happens only when i am not defining the variables before its use.
As in php there is no need to declare the variables before using.

it is necessary to define variable at every time or checking the variable is set or not.

Does anyone knows how to hide this error.
please help me to solve this problem.


Thank You,
Amol Kharat.

#2
[eluser]bigtony[/eluser]
If you do this:
Code:
$x = $abc;
Then you will get an error if $abc was not previously declared. So all you need to do is this:
Code:
$abc = 'hello';
$x = $abc;
Now $abc will be found, and $x will also be available for later use.

Does that answer your question?

#3
[eluser]jedd[/eluser]
[quote author="Amol Kharat" date="1253967930"]
Message: Undefined variable: abc
[/quote]

Show code. Generally the only place I get these are when I try to add to an array (and it's not already been declared).

#4
[eluser]firefly_HIT[/eluser]
check this file: php.ini
find this:error_reporting = E_ALL & ~E_NOTICE

it means - Show all errors, except for notices

maybe yours is like this
error_reporting = E_ALL
means show all message ,include notices


as what you had described ,it is just a notice,not an error.
so ..be easy...

#5
[eluser]cahva[/eluser]
No, definately keep the notices on! Even if PHP allows you to use undefined variables, it doesnt mean that you should use undefined variables. Always define variables that you use. Dont go where the fence is the lowest Smile

If you use some variable that might not be defined all the time, use isset function to check if it set:
Code:
if (isset($abc))
{
    echo $abc;
}

#6
[eluser]BrianDHall[/eluser]
[quote author="Amol Kharat" date="1253967930"]Hello all,

I am new in codeigniter and i have problem:

-----------------------------------------
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: abc

Filename: views/p_dashboard.php

Line Number: 19
----------------------------------------

This happens only when i am not defining the variables before its use.
As in php there is no need to declare the variables before using.

it is necessary to define variable at every time or checking the variable is set or not.

Does anyone knows how to hide this error.
please help me to solve this problem.


Thank You,
Amol Kharat.[/quote]

OK, the problem is that PHP actually ALLOWS you to refer to a variable before it is declared, but is will still issue a NOTICE error. The new PHP recommendation is to NEVER refer to a variable that has not yet been declared, however your applications will still function regardless of whether you choose to initialize them or not.

You can use is_set() or empty() functions to check a variable for truth without generating an error.

In your index.php in the main CI distribution you will also find:

Code:
/*
|---------------------------------------------------------------
| PHP ERROR REPORTING LEVEL
|---------------------------------------------------------------
|
| By default CI runs with error reporting set to ALL.  For security
| reasons you are encouraged to change this when your site goes live.
| For more info visit:  http://www.php.net/error_reporting
|
*/
    error_reporting(E_NOTICE);

You can change the error reporting level, but note that just because you don't see an error does not mean PHP will not generate one. This can become a considerable performance issue if you are doing this a lot, as you might be generating 2-10+ PHP notices/errors every single time anyone loads any page on your site.

You are indeed free to hide the notices, but know that it is considered bad practice, is generally frowned upon, and will cause a performance hit down the road and you can simply solve it by using empty() or is_set() php functions.

#7
[eluser]Nikhil Dev[/eluser]
I have pretty much a similar question, but this thing is pissing me off.

I have a controller called blog.php containining

Code:
<?php
class Blog extends Controller {
    function index() {
        $username = 'numsdf';
        $this->load->view('blog_view', $username);
    }
}

And my view blog_view.php contains the following
Code:
<html>
<body>
<?php
    echo 'Hello' . $username;
?>
</body>
</html>


its the simplest example. But this always has been returning an error
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: username

Filename: views/blog_view.php

Line Number: 4

Initially I though this was just something to do with my php(since I was on php 5.3). But after I saw the update on the website that CI1.7.3 is compatible with PHP5.3, I redownloaded the whole thing and tried it. No luck Sad

any idea?

I tried this on 3 other machines (Win, Mac and Ubuntu). All return the same thing. Any idea why this is happening?

I love the way CI work(very similar to django). Someone please help me.

#8
[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...views.html

Code:
$data['username'] = 'numsdf';
$this->load->view('blog_view', $data);

#9
[eluser]Nikhil Dev[/eluser]
That worked. I tried that earlier as well, but didnt work then. But I think I was also the version thingy. But good news is that it works now


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.