I used to code in C# before starting to PHP and I am quite new to PHP to be honest.
In C# it was a total must-do to do exception handling or app frozen, crashed and than maybe even system crashed and user was pissed off and deleted the crap from PC.
I am coding now in PHP for 2 months, without using try,catch and it has been working for me just fine (I didn’t know exception handling existed till today really, so I just did simple stuff like if(!$result){die(“problem bla bla”);}
So my question is, where and when should I use try, catch? Where is it suggested to be used, maybe with sql queries or file handling or some other stuff? Is it necessary to use or optional?
Creating a quick PHPInfo page in a restricted directory which calls the phpinfo() function will tell you which PHP ini files are loaded. This is the easiest way to check which of those myriad of files is correct.
Just don’t leave it up, as it exposes a lot of information about your website.
Alternatively, you could create a script and run it from the command line for the same result.
Sorry to be still OT, but this happens to everyone. You just need to learn to pinpoint the errors from the error messages. Case in point, someone tells you to type something, you type it, it does’t work. Option 1: advice is faulty. Option 2: You made a mistake - there is no disease that stops you checking letter by letter. Option 3: understand what the original instructions meant and correct the OP thanks to new knowledge (in this case: locate <file>)
If Eng is your second language, you’re pretty good but remember we struggle also. Can’t think of an example, but I come across words I’ve been misreading/mispronouncing in my head often.
Back on topic, as Scalio said, you need to test the out.
Sorry, I have the problem with words, usually switching letters or writing words in wrong order. I think they call it dyslexia or something.
It’s actually called “not checking what you typed”
If you actually had dyslexia you would’ve checked letter by letter and found your error. But instead you just typed some stuff and came back to forums when it didn’t magically solve everything.
Firefox checks spelling mistakes for me. I don’t have so huge problems with it, but still, yesterday I spent 10 minutes debugging some function when I decided to check queries via phpmyadmin and saw I wrote gnvtools instead of gvntools. Stuff like this happens to me all the time, when I was working in C# I always got undefined variables errors for mispelling.
And when I learn new English words, I usually always read them wrong, like I remember when I learned word “subtitles”, but I read it as “subitles” and than noone understool what I wanted to say LOL. Or @ biology I read “escherichia coli” as “escherchia coli” and than used it and peeps tought it was funny.
Anyway, lets go back to topic. Which php.ini is correct?
I don’t know about Zend, as I’ve never used it, but the framework I use (Yii) uses them quite a lot.
I suppose Zend does as well, as do other (PHP5) frameworks *.
That’s good practice; never show anything about the internal workings of your website to users. Anything they get to know gives them more information on how they should hack your website.
Alternatively, you might want to consider having a constant variable declared somewhere like DEBUG_MODE and a debug function that changes behavior according to the value of this constant.
That is, if DEBUG_MODE is true (enabled) show a descriptive message, and optionally a back trace, whereas it only shows a plain and simple message like “500 - Internal server error” when MODE_DEBUG is false (disabled). That way you have a good of debugging when you need it, which can be easily turned off by setting DEBUG_MODE to false when you go live.
PHP 4 and lower don’t have exceptions and try/catch.
Hmmm, so its an optional thing. What about if you use frameworks like Zend, should you use them there?
One of my friends easily hacked the PHP website to get “restricted” content just because website was showing exceptions/errors on mqsql query. So I always delete all echos for results, errors or requests after I am finished with a function.
It’s an ongoing debate in the PHP community, but as it stands now PHP itself (the core) doesn’t throw any exceptions as far as I know.
Instead, it returns false (for functions like strpos), or null (for functions like mysql_fetch_assoc).
The exceptions can be used to create your own error handling might you want to do so.
Well, I’m not sure which one is correct, but all the ones not ending in .ini, i.e., the .ini-recommended files, are most probably not the correct ones.
My guess would be:
/usr/local/cpanel/3rdparty/etc/php.ini
But I’m not sure.
To test it, you can create a very basic file with only
<?php
echo "Bingo!";
?>
and set that file to be auto prepended to every request using auto_prepend_file.
If you’ve named the file /home/my/test.php
Set
auto_prepend_file=/home/my/test.php
in one of the php.ini files, restart apache, and check a website. If it says “Bingo!” on the top you’ve found the correct one
PS. If it’s a live server I can imagine you don’t want visitors to see the bingo, in which case you could do echo “<!-- BINGO –>”; so it won’t show on the website, but it will in the source.