Please see the below code this is work perfectly in PHP. Can anyone explain me how this code works. Because in the below code i have declared $caregory_id without semicolon and without any value deceleration. Then also this code work perfectly without any error and var_dump($category_id) returns me an null value.
How php execute this code without semicolon??
<?php
$category_id= //No semicolon
var_dump($category_id); //returns NULL
?>
It works because PHP treats your code like this:
$category_id = var_dump($category_id);
The return value of var_dump() gets assigned to $category_id. Undefined variables in PHP are implicitly set to null, which is what you see in the output of var_dump(). However, you would also get a notice about $category_id not being defined; if you don't see it, you should use this code in your script:
error_reporting(-1);
ini_set('display_errors', 'On');
These settings are also recommended during development as they can catch issues that would otherwise have gone unnoticed on a production machine.
Related
I'm attempting to debug the PayPal review process in Magento. Every time I try to dump the following variable I get a white page:
//the variable declaration:
$shippingAddress = $this->getShippingAddress();
//the dump that breaks the page:
<?php echo '<pre>';print_r($shippingAddress);echo '</pre>'; ?>
I also tried with a variable on the page that was being used for something other than if statements.
//this variable displays results
<?php echo '<pre>';print_r($billingBlock->setShowAsShippingCheckbox(true)->toHtml());echo '</pre>'; ?>
//however, this one does not:
<?php echo '<pre>';print_r($billingBlock);echo '</pre>'; ?>
I was just wondering what might cause my var_dump to break the page? How do I see what is in the object if I can't dump it?
First, PHP never "just white pages". When you get a blank screen, that means PHP's execution has halted fro some reason. However, unless your server has been configured to not log errors, the PHP error log or the Magento exception log should have an error for you.
As far as your specific problem goes, many of Magento's objects contain reference to a large amount of information — and sometimes the references are circular. PHP's var_dump and print_r functions will blindly follow these circular references and attempt to print everything out. This eventually leads to PHP using more memory than is allowed by the memory_limit ini setting, and execution halts.
Most PHP professionals use the xDebug extension to work around this. The xDebug extension has a modified var_dump that will limit the amount of information dumped, which prevents the above memory limit problems. The xdebug.var_display_max_children, xdebug.var_display_max_data, and xdebug.var_display_max_depth ini settings are the ones you'll want to tweak if xDebug's still not helping with the memory limit problem. (some PHP distributions have these set too high initially)
If that's not a possibility, a little caution with your var_dump's can still help.
Use this to figure out the variable type
var_dump(get_class($thing));
If it's a Magento object, use this to see its data keys
var_dump(array_keys($thing->getData()));
And then output individual data members with
var_dump($thing->getData('key_name'));
var_dump($thing->getKeyName()));
A PHP parse and fatal error would produce this. You may want to have a look at your error log.
You could try adding the following lines to the beginning of your php files (just after the opening PHP tag "
ini_set('display_errors',1);
error_reporting(E_ALL);
I have a link that points to a webpage e.g. "land.php".
The link looks like this:
link
this takes me to the page land.php where I can read the first parametere with $id (and it is equal to 1, correctly), but I cannot read the second one. I either tried with $cd or $_GET['cd']. None of them works.
if I tried isset($cd) it says false. Same thing for isset($_GET['cd']).
How can I pass the second parameter too (and read it!)?
EDIT:
some code (so people are happy. I think it's pointless in this case..).
land.php
<?php
if($_GET['cd']==a)
echo "<h2>HI</h2>";
else
echo "<h2>BY</h2>";
?>
if I use $cd instead of $_GET['cd'] it doesn't work anyway..
EDIT2 I don't get any syntax error, it just doesn't behave how expected.
The value is stored in $_GET['cd'].
Try printing out the $_GET array, with print_r($_GET);
print_r($_GET) should output
Array
(
[id] => 1
[cd] => a
)
This should ofcourse be in the land.php page, as the get variables are only available in the requested page.
Your server might be set up to accept semicolon instead of ampersands. Try replacing & with ;
$_GET['cd'] is the correct syntax. Are you actually on the land.php page, ie does your browser's address bar read something like
example.com/land.php?id=1&cd=a
Also, it looks like you have register_globals enabled if you can read $id. This is a very bad idea.
Update
Your code snippet contains syntax errors. I recommend the following, including enabling decent error reporting for development
ini_set('display_errors', 'On');
error_reporting(E_ALL);
if(isset($_GET['cd']) && $_GET['cd'] == 'a') {
echo "<h2>HI</h2>";
} else {
echo "<h2>BY</h2>";
}
I have a weird problem, I am using print_r($obj) in Joomla YOOtheme ZOO extension and it returns a blank page. it just act as die() !
it should output the object but it does not.
Please note that print_r() is working fine with some other objects and variables.
I am using XAMPP on Windows.
Any help?
Upon executing print_r() and var_dump(), the page is just blank, no error, view source shows:
<html>
<head></head>
<body></body>
</html>
Error reporting is turned on.
It is posible that $obj is too big to load, and grabs a lot of memory after which the script stops to work.
Thanks
Is error reporting turned on?
If not add to your code:
ini_set("display_errors", "1");
If it's outputting nothing, $obj contains nothing. Try var_dump() instead. Also, make sure you're seeing all errors that PHP is producing:
ini_set('display_errors', 1);
error_reporting(-1);
On my localhost, if I do echo $a->b where $a is not an object, it shows nothing. But on another server, it gives me an error of "Trying to get property of non-object". How to ignore this error? I just want a quick and easy way to make the code running, and I know what I am doing.
If you just want to not display this notice you have the option of turning off the notices by including this snippet at the beginning of your script:
error_reporting(0);
// or error_reporting(E_ALL & ~E_NOTICE); to show errors but not notices
ini_set("display_errors", 0);
This could also be setup globally in the server settings.
Alternatively you can ignore the notice on a specific line by using the # symbol, like so:
echo #$a->b;
That would suppress the notice for that single statement.
The third, most 'proper', most time consuming, and most workload expensive option would be to add checks around each such code segment, to ensure it has been set prior to trying to read it, Jacob's suggestion.
try use something like
error_reporting(E_ALL & ~E_NOTICE);
in first line of your code.
check the php.ini, in the section of errors.
This is a bit of an oddity for me. PHP is my forte, and I can normally figure out any issue I encounter.
I have a custom framework that I have been using for years. I have taken it upon myself to rewrite it, and I'm doing everything essentially the same that I was before. The problem lies in the following construct:
function ModPages_GetPage() {
$page = ModPages_GetPageByName($_GET['page_name']);
if($page != false) {
include(TPL_DIR.'pages/pages.view.php');
} else {
ErrorMessage('Invalid Page', 'The selected page could not be found.');
}
}
function ModPages_GetPageByName($page_name = null) {
$db = new Database;
$query = '
SELECT *
FROM pages
WHERE page_name = "'.CleanStr($page_name).'"
AND page_enabled = "yes"
LIMIT 1
';
$page = $db->GetRow($query);
return $page;
}
This code is being called with 'home' for the value of $_GET['page_name']. The call to ModPages_GetPageByName() is working fine, but the value of $page in ModPages_GetPage() isn't getting set. Matter of fact, any debugging statements thrown in after that call are failing to display anything.
I have display_errors set to on, and error_reporting set to E_ALL. I get a couple notices from my Database class, but that's it.
Running the script at a shell fails to produce any errors. When using strace, I do see the process spits out an 'exit_group(255)'.
This one has me quite baffled. I could sure use some direction on this.
I would think it's your query, shouldn't you just return the page name instead of star? as star (*) would return an array which is probably being passed back as the value? just my guess.
$query = '
SELECT *
FROM pages
WHERE page_name = "'.CleanStr($page_name).'"
AND page_enabled = "yes"
LIMIT 1
';
if you do a print_r on the $page return I would think it should be an array
$page = $db->GetRow($query);
echo "Page:<pre>".print_r($page,true)."</pre><br />\n";
Then maybe return something like this
return $page['page_name_field'];
ok before we get to a solution can we first make sure that before setting the $page variable, first just echo $_GET['page_name'] to see if there is a value being received.
PK
Does your script stop right after your database call, or just doesn't display any output?
If the first is true, then it looks like a fatal error. With E_ALL, it should be displayed, are you sure both display_errors and error_reporting are as you say at that point, and that the GetRow function doesn't alter them in any way? If so, maybe there's something in the Apache error log (PHP errors are sometimes logged there).
If the latter is true I'm thinking about an exception being thrown in a method that is being called, and caught in a higher level function. To check this you can put the database call (ie: the point where things go wrong) inside a try/catch block and see if you reach the catch block.
I would try following:
replace $_GET with $_REQUEST (maybe your form is using POST?)
do a print_r to check contents of your variables.
use mysql_error to view any errors, or print your mysql query in your browser, copy/paste it in phpmyadmin, is it returning anything? error.. data?
something similar happend to me once, my framework was encoded in ANSI and my calling php file was UTF8+BOM... I changed everything to UTF8+BOM and it worked.
try also different browser, I know it might not be a browser problem, but it might be that your script is cached somewhere.
are you using some caching? like eaccelerator?
Are those functions in a class? If so, you will need $page = $this->ModPages_GetPageByName().
Also I would echo out the argument and the sql statment in ModPages_GetPageByName(). This way you can verify that it isn't a SQL error.
I can't say for sure why your code isn't working, but I can make some suggestions that might help in locating the error.
The first thing I notice is you don't check that $db actually contains a valid database. I don't know the details of your Database object but I'm assuming there's some mechanism in there for checking if it's actually connected to the database. You should use that to determine if the database is connected before running queries on it.
$db = new Database ();
if ($db -> isConnected ())
{
$query = 'SELECT * (etc etc etc)';
// ...
}
else
{
// Put some kind of DB connection error notification or throw an exception here
}
Just on a stylistic note, you don't need to store the results of your DB lookup before returning it, unless you're planning on doing some processing on the result before returning it. You can just return the lookup directly. Of course that's just a stylistic choice, but it saves a line or two :)
return ($db->GetRow($query));
After you run your getpage function, I'd strongly recommend var_dump()ing the result. Even if your function returned NULL, you'll still see this in the var_dump. If in doubt, dump it out :). I'd also recommend installing xdebug to make the var_dump output more readable.
$page = ModPages_GetPageByName($_GET['page_name']);
var_dump ($page);
I would also strongly recommending var_dumping your query before you execute just to make absolutely sure that you're running the query you think you're running. Copy and paste the outputted query into sqlyog or phpmyadmin or whatever you use for interactive access to your database and make sure it returns what you think it should return.
Other things to check, is the page you're trying to return actually set page_enabled='yes'? Does the page_enabled column actually store the value as 'yes', or is it a bool or an integer or something else? Is magic quotes enabled or disabled? If they're in one state when you think they're in the other they can cause confusion. Are errors actually being reported to the browser? Add a line at the top of your script that's guaranteed to fail just to make sure, like an attempted foreach on an integer. If you don't see an error, then maybe error reporting isn't configured properly. I know those are obvious questions but I also know how easy it is to overlook the obvious if you're not getting what you expect out of a query.
Are you sure $page is not set, or is it just that your debug instructions don't print anything? Try logging to a file or a database instead; maybe your code triggered output buffering or something like that.
Also, you are calling ModPages_GetPageByName before declaring it. That is usually okay, but might not be in special circumstances (e.g. when the code is wrapped in an if block). Try swapping the two.
Also, check your environment and disable opcode caching and other possible error sources. APC for example can call the old version of the script long after you changed the PHP file.
While some of you have put extra effort into responding to this, nobody has been able to see the full picture, even given the details I have provided. I have been unable to trace the issue back to its source, but have moved on to a different project.