Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Matthew Carter Newsgroups: comp.lang.php Subject: Re: PHP Warning: extract() expects parameter 1 to be array Date: Tue, 23 Aug 2016 01:26:34 -0400 Organization: Ahungry (http://ahungry.com) Lines: 76 Message-ID: <87oa4kukc5.fsf@ahungry.com> References: <2d416b5e-e923-4d44-bcbf-6956d2436ef2@googlegroups.com> <200820161821375934%timstreater@greenbee.net> <18d88f90-6b3b-4e2f-8b9f-5cb9c69176a1@googlegroups.com> <210820161026199601%timstreater@greenbee.net> <87pop2unf0.fsf@bsb.me.uk> <10692138.PaQTzsjEax@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="7c986cd4736462de309a749b207746fe"; logging-data="27801"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19R9M0hMVJ3mm6lNXDZ4hrh" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:Tao4DDy3Epz8jomD3hqhtaYt1EI= sha1:Y/PP7GusULdDT+/fe9qAj0odV8U= Xref: csiph.com comp.lang.php:16975 Jerry Stuckle writes: > On 8/22/2016 3:34 PM, the troll Thomas 'Pointed Head' Lahn wrote: >> Ben Bacarisse wrote: >> >>> […] I prefer the "more structured" version: >>> >>> if ($argc < 2) >>> echo "Please, provide a path to a file\n"; >>> else if (!file_exists($file)) >>> echo "Sorry, there is no such file $file\n"; >>> else if (!is_readable($file)) >>> echo "$file is not readable\n"; >>> ... >>> else if (($open_file = fopen($file, "r")) === false) >>> echo "Sorry, couldn't open $file\n"; >>> else { >>> // All ok. Let's get on with it... >>> >>> while (...) >>> { >>> // Main work is here >>> } >>> } >> >> This code style is a maintenance nightmare (the missing blocks and the >> assignment in the “if” statement doubly so). Do you know what the condition >> on line 50 was because of which you wrote “else” on line 100? >> > > Yes - if the code is properly indented and commented (something you > wouldn't understand). > >> Also, PHP is influenced by C, but it is _not_ C. The function should throw >> (PHP Standard Library) exceptions like \InvalidArgumentException instead of >> echo-ing error conditions without regard to the user’s locale. This will >> also get them a stack trace (even better if the Xdebug extension is >> installed and enabled) if they do not catch the exception. As a result, the >> then-unnecessary “else” statements would vanish in a puff of program logic. >> > > Which would display to a user internal details of your code - a huge > security exposure. But you don't understand security, either, do you? > > In addition, it will immediately stop processing of the page - resulting > in invalid HTML being sent to the browser. Something else you don't > care about, either. > >> >> > > Glad you can cut/paste a URL. Too bad you don't understand how write > good PHP code. Details of stack traces are only exposed if the environment (production?) is configured to match that of a developer environment (such as showing errors vs logging them). It is also quite easy to wrap your outer-most layer of code input/output in a try/catch block and handle: - Rendering a generic or descriptive error message for the user in the catch block - Manually logging the stack trace (part of the \Exception object) to a file or sending as part of an email to the webmaster (if this is not configurable in the php.ini of the server for some reason) - Ensuring the HTML the user receives is still valid This also allows for easier to extend code in the future, as you end up with a single exit point. -- Matthew Carter (m@ahungry.com) http://ahungry.com