|
Not trying to start a flame here, I've just gotta vent: Sigh, I am now responsible for maintenance of an old PHP app, for some reason. I can do PHP, but there are other people at the company who are much more used to its idiosyncrasies. I never really understood all the hate for PHP programmers but now I think I'm coming around. This is admittedly an old project but goddam, I'm hoping that maybe I can talk about some things that annoy me and you guys can tell me why it actually makes sense in a PHP context. PHP Tag Fairy Dust Going in and out of PHP, I know its a templating language but gently caress! I cannot handle it. code:
Not using classes Anytime I've done websites in PHP its used classes. Is there a reason not to use classes? I think PHP has pretty good object oriented system going in PHP5, is it just the blockheads I follow up that don't use it? It makes way too much sense to use classes? Am I just broken from Python and relying on classes for web design when they are actually a hindrance?
|
# ? Feb 7, 2012 06:59 |
|
|
# ? Jun 4, 2024 16:22 |
|
Innocent Bystander posted:PHP Tag Fairy Dust As a preface, I use PHP sparingly, and typically just when I need to write up a simple database manipulation frontend for personal use. I don't get how this works, isn't this variable assignment that evaulates to true always? And what's meant to be in $out anyway? code:
code:
|
# ? Feb 7, 2012 07:21 |
|
quote:Is this normal coding style? There aren't too many alternatives that make sense. Keep in mind that echo is a language construct and can take a list. This pattern has become popular at work: php:<?php echo '<select ...>'; foreach($records as $row) { echo '<option', ($row['selected'] ? ' selected="selected"' : ''), ($row['value'] ? ' value="' . htmlspecialchars($row['value']) . '"' : ''), '>', htmlspecialchars($row['derp']), '</option>'; } echo '</select>'; Switching in and out does feel a bit better when you're able to turn on short tags and can use <?= to echo. Short echo is always enabled as of PHP 5.4. quote:Is there a reason not to use classes? Also, keep in mind that OO is not a magic bullet. It helps when building complex systems, but you don't need some big complex MVC framework to do day to day tasks. Use objects to manage complexity, not create it. McGlockenshire fucked around with this message at 07:34 on Feb 7, 2012 |
# ? Feb 7, 2012 07:30 |
|
Can someone explain when it is appropriate to use closures/anonymous functions in PHP. Obviously you don't NEED to, but I'd like to understand when it would make sense to use them. This could probably apply to any language. I use it all the time in jQuery, but only out of 'this is how other people do it' rather than an understanding of the concept.
|
# ? Feb 9, 2012 00:08 |
|
revmoo posted:Can someone explain when it is appropriate to use closures/anonymous functions in PHP. Obviously you don't NEED to, but I'd like to understand when it would make sense to use them. This could probably apply to any language. I use it all the time in jQuery, but only out of 'this is how other people do it' rather than an understanding of the concept. In general, you give names to things for two reasons: Either the name improves understanding for someone reading the code, or you need to use the thing more than once. This applies to all kinds of values, be they numbers, strings, arrays, objects, or functions. So, if you use a function more than once, it should have a name. If a function's purpose is not obvious from looking at it, it should probably have a name that helps convey what its purpose is. If a function definition is long and ugly and would interfere with reading the code around it, then give the function a name and move its definition somewhere it doesn't make reading the code hard. Single-use functions with obvious use that fit into the flow of the code around their usage site are excellent candidates for becoming anonymous functions.
|
# ? Feb 9, 2012 00:41 |
|
So, something small that you would only do once in a specific piece of code that doesn't warrant its own full function? Am I barking up the right tree? That actually makes a lot of sense. It's just an easy way to encapsulate a few lines of code that don't deserve their own actual function inside other logic. Right? I could see where that would come in useful.
|
# ? Feb 9, 2012 01:34 |
|
revmoo posted:So, something small that you would only do once in a specific piece of code that doesn't warrant its own full function? Am I barking up the right tree? That actually makes a lot of sense. It's just an easy way to encapsulate a few lines of code that don't deserve their own actual function inside other logic. Right? I could see where that would come in useful. code:
The Gripper fucked around with this message at 02:14 on Feb 9, 2012 |
# ? Feb 9, 2012 02:10 |
|
ShoulderDaemon is onto something with the idea behind naming. With functional languages, the idea of doingcode:
code:
Like most things with PHP, anonymous functions are just crowbared in there and are awkward to use. Look into a real functional language (or at least one where functions were first order citizens to start with, like LISP or Scala) and you'll get a better feeling for how it's done well.
|
# ? Feb 9, 2012 03:07 |
|
Innocent Bystander posted:Going in and out of PHP, I know its a templating language but gently caress! I cannot handle it. Well; as you said PHP is for templating really. Adn so the injection of <?php echo $whatever; ?> into HTML blocks is a key feature of the language, so yes people do it. However; what you have there is a combination of almost obfuscated code structure, and a stupid mixing of decisions and templating. The fact that that's been interwoven through HTML is just the straw that breaks the camel's back there. Innocent Bystander posted:Anytime I've done websites in PHP its used classes. Is there a reason not to use classes? I think PHP has pretty good object oriented system going in PHP5, is it just the blockheads I follow up that don't use it? Again; 2 things. It is in fact a logical fallacy to think that OO design is inherently 'better' than procedural design. So if an engineer is palpably better at procedural design and decomposition; they have a pretty compelling reason to go that route. (your previous example hints that the standard of procedural code you're dealing with will also be poor, though) Second; how old is this legacy code? Because in PHP 4 the OO implementation was pretty awful - and certainly not bothering with it was a more believable proposition than it is now.
|
# ? Feb 11, 2012 00:31 |
|
revmoo posted:So, something small that you would only do once in a specific piece of code that doesn't warrant its own full function? Am I barking up the right tree? That actually makes a lot of sense. It's just an easy way to encapsulate a few lines of code that don't deserve their own actual function inside other logic. Right? I could see where that would come in useful. You'd want them to be small; otherwise the local (calling) code will be a mess. You'd want them to be specialised; if its too generic then really its something that's worth factoring out for the likely re-use. (I keep a library of common functions to throw into array_map, array_filter and array_reduce)
|
# ? Feb 11, 2012 00:37 |
|
So out of the blue I'm having a problem with images loading on my website. The data for these is pulled from a database and displayed. Works great, and has worked for a while, in chrome and firefox. Now for whatever reason it decides to not display the images in Internet Explorer. I have no idea what the hell happened, no one has access to change anything but me I don't know how to fix it. This is our busy season (tax returns)
|
# ? Feb 11, 2012 02:41 |
poxin posted:So out of the blue I'm having a problem with images loading on my website. The data for these is pulled from a database and displayed. Works great, and has worked for a while, in chrome and firefox. Now for whatever reason it decides to not display the images in Internet Explorer. I have no idea what the hell happened, no one has access to change anything but me I don't know how to fix it. This is our busy season (tax returns) Can post post the portion of the script that is sending the image data to the browser? Is there a public link we can use to reproduce the issue ourselves?
|
|
# ? Feb 11, 2012 03:01 |
|
poxin posted:So out of the blue I'm having a problem with images loading on my website. The data for these is pulled from a database and displayed. Works great, and has worked for a while, in chrome and firefox. Now for whatever reason it decides to not display the images in Internet Explorer. I have no idea what the hell happened, no one has access to change anything but me I don't know how to fix it. This is our busy season (tax returns) Make sure you're sending the right headers, would be my first guess. Maybe a newer version of IE can't handle a header properly. Post some code if you want more help.
|
# ? Feb 11, 2012 03:02 |
|
Edit: I updated my theme which in turn updated timthumb. Solved the problem
poxin fucked around with this message at 04:26 on Feb 11, 2012 |
# ? Feb 11, 2012 03:38 |
|
We have an Adobe Flex text editor for viewing internal messages. Sometimes, randomly, it does not fully display. The background for it shows up in the MXML but the editor does not. Some debugging has led me to find out that, sometimes, the session is disappearing. I don't yet know if this is linked, since it doesn't tell me who it's happening to, but it's my only guess at the moment. The code has this curiosity in it: code:
And, would session_unset() and session_destroy() cause notices if there were no session, hence the error suppression? Golbez fucked around with this message at 19:00 on Feb 13, 2012 |
# ? Feb 13, 2012 18:55 |
|
Learning some php over the weekend.. started adapting some code I found online to expand upon it. The page won't even load and it validates on the validator.code:
|
# ? Feb 14, 2012 05:44 |
|
Please don't use any of that (-- as in, that whole thing needs to go in the trashcan. Don't bother "fixing it up"). You're vulnerable to every single vulnerability that literally exists. MD5 isn't "encryption" and it's fairly worthless. Impotence fucked around with this message at 05:50 on Feb 14, 2012 |
# ? Feb 14, 2012 05:48 |
|
Biowarfare posted:Please don't use any of that (-- as in, that whole thing needs to go in the trashcan. Don't bother "fixing it up"). Awesome. Is there an up to date example/tutorial to follow. I've coded before and it's easy to addon later, but I need some basic functionality now.
|
# ? Feb 14, 2012 05:51 |
|
Hello, I have a question that I'm sure is pretty simple but after scouring the PHP manual I have not been able to figure it out. Basically, I'm trying to create multidimensional arrays with named keys instead of numbers. As a practice exercise I'm trying to create a league standings board like you might see on a sports site. I want to have an array for $teams and then assign each team a key inside of the array and then create arrays for players and stats, then for each player create an array for there stats and so an so forth. I imagine this could be done better with a database but I'm still learning my php basics. Here is my best attempt so far, followed by the output: code:
code:
Any advice is greatly appreciated.
|
# ? Feb 14, 2012 06:42 |
|
I'm confused by what you're looking for, so I'll go with the absolute most basic: It's very possible to have named keys, and it's easy to make arrays within arrays. Like so: $array = array('foo' => array('shazbot' => 'a', 'snert' => 'b'), 'bar' => array('lol' => 'dongs')); Edit: And as for why your foreach loop isn't updating: when it runs, foreach creates a copy of the array, so you are not editing the actual array. If you want to do it that way (and I'm not sure I recommend this), you need to pass it by reference, like so: foreach ($team AS $key => &$value) That way, when you edit $value, it actually edits the place in the memory where $value is (that is, the original $team), instead of the temporary copy created by foreach. Golbez fucked around with this message at 07:13 on Feb 14, 2012 |
# ? Feb 14, 2012 07:11 |
|
Basically I want to build an array that looks like this:code:
code:
|
# ? Feb 14, 2012 07:27 |
|
If you wanted the simplest solution you could just use an array of Team objects. You would have your Team class with properties like Name, Points, Wins, Losses and whatever else you want, then create objects and iterate over an array of them. This Team class is for demonstration only, if you aren't familiar with OOP and concepts like scope, you should probably look into it. The PHP 101 topic on OOP is pretty average but should give you an idea. I'm sure if you ask in here there will be some recommendations for material on OOP. code:
code:
e: If you wanted to add players, create a Player class then do the same thing with players. In the Team class have an array of Players that belong to that team, then when you iterate over the team with a foreach you can nest another foreach to iterate over each player in the team etc. Sorry if this wasn't what you were after, but I think you were trying to take an unnecessarily complicated route by trying to jam all this information into multidimensional arrays. If you want to take it even further, look at a small framework which will map database tables to objects for you and separate your logic and view. I know it can seem overwhelming (especially if you are just starting with the language, even more so if this is your first programming language) which is why I didn't mention it from the start. Xik fucked around with this message at 12:15 on Feb 14, 2012 |
# ? Feb 14, 2012 11:52 |
|
Ninja Dan posted:Any advice is greatly appreciated. Without wanting to give advice on how you should architect the system you are building, I think I can try to advise a little on how arrays work. Basically an array in PHP is an associative array, meaning the keys are strings, so you have things like array('butt' => 'poop', 'nose' => 'boogers'); but for many applications you have no need of descriptive labels for array elements, so arrays with numeric keys (starting at 0 and counting upwards) are common. When you use the $existing_array[] = ...; syntax, you are telling PHP to add a new element to $existing_array and that the key for that new element should follow on from the maximum key currently found in the array. I'm sure there are rules for how the new key is determined, but that's basically it. So if $teams is an empty array, when you do $teams[] = array(...); you are adding a new element to $teams, whose key will be 0, and whose value will be the new array. So you could get by with something like code:
If you want to eventually have an interactive application where users can request stored information about teams then you are going to need to use a database. You then need to tackle the problem of fetching the data from the database into your application. However you decide that you want the data represented in your application, there is a way to assemble the data from the database in that form, but some ways of doing it will be less hassle than others.
|
# ? Feb 14, 2012 16:00 |
|
simcole posted:Learning some php over the weekend.. started adapting some code I found online to expand upon it. The page won't even load and it validates on the validator. I don't want to go into detail about every fault that can be found in this code, but one specific fault is that addslashes() is not sufficient to escape data for a database query. At a bare minimum you need to use an escaping method that is tailored to the specific database connection that you are working with. The main comment that applies to this code is that it's clunky, hard to read and you don't want to be writing a whole site like this.* If you want to build more than a "quick and dirty" script then as a bare minimum you need to organise common functionality that will be used throughout your application into classes and/or functions. If you want to build a big site then consider learning a framework. People will recommend frameworks I'm sure. * As an example, you really don't want to be building a big old site and have every page filled with stuff like if (get_magic_quotes_gpc()) again and again. Another observation is that you want to be able to separate the HTML output of your page from the business logic of your application where you can, which the code you posted doesn't really achieve. (The reason why I'm assuming you want to build a sizeable application is because your example code seems to be for a login page, or something similar, which seems to imply a large site. If you're just learning, my assumption might not be accurate.)
|
# ? Feb 14, 2012 16:12 |
|
Golbez posted:The code has this curiosity in it: session_destroy() is effectively a no-op unless the session already exists. Calling it without an existing session is entirely unnecessary at best and may cause undefined behavior at worst. Worse, calling it when you've defined the session id but haven't loaded the data is simply insane. They both may be throwing notices about the bogus conditions. You can safely delete session_unset. You can modify the call to session_destroy to act only when a session currently exists. code:
|
# ? Feb 14, 2012 18:48 |
|
Good point on that. Can you think of any reason why it would be setting the ID, then recreating the session, instead of just, y'know, starting a session?
|
# ? Feb 14, 2012 19:14 |
|
Hammerite posted:magic quotes http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2012-0831 Last revised:02/13/2012
|
# ? Feb 14, 2012 20:51 |
|
Golbez posted:Good point on that. Can you think of any reason why it would be setting the ID, then recreating the session, instead of just, y'know, starting a session? Total guess here but it feels like a half-assed attempt at load balancing/outdated session management.
|
# ? Feb 15, 2012 00:15 |
|
Does anyone host with Fatcow? I'm having the toughest time trying to understand this guide on how to copy and paste the php.ini script. http://www.fatcow.com/knowledgebase/beta/article.bml?ArticleID=1290&type=How%20To#Nugget_1374 My document root is... /home/users/web/b24322/moo.username Notice the "moo" in "moo.username" The tutorial says that the abbreviation for Fatcow is supposed to be "pr" but for me, it's "moo." So I assume I'm supposed to put "moo" in place of "pr" whilst following the guide. Here's what I did... 1) Created a new folder called "cgi-bin" 2) Nested a folder called "temp" inside of cgi-bin. 3) Created a document titled "php.ini" 4) Copied and pasted php.ini from the link provided in the guide. 5) Changed the "sessions.save path =" line to read session.save_path = /home/users/web/b24322/moo.username/cgi-bin/tmp Obviously the tutorial says to put "pr.username" instead of "moo.username" but I assume that since I see "moo.username" in the document root category of the server information page, that's what I'm supposed to put. I've already tried it with "pr.username" and it didn't work either. Anyway, once I get all that done, I go to the "PHP Scripting" page and try to set the version, but it won't bring up a list of versions. The drop down menu is just blank.
|
# ? Feb 15, 2012 02:55 |
ZanderZ posted:Does anyone host with Fatcow? I'm having the toughest time trying to understand this guide on how to copy and paste the php.ini script. They have a phone number for support as well as live chat, I would suggest trying that since you are paying them money.
|
|
# ? Feb 15, 2012 03:28 |
|
Golbez posted:Good point on that. Can you think of any reason why it would be setting the ID, then recreating the session, instead of just, y'know, starting a session? Given that it's Flex and therefore probably a Flash front-end, it's very possible that Flash doesn't pass through the user's cookies properly, but it can read them and thus pass it along.
|
# ? Feb 15, 2012 06:16 |
This is more of an htaccess / apache question, but I couldn't find a more appropriate thread, so here goes: I'm using a little Wordpress plugin that adds watermarks to images in the uploads directory by including an htaccess file that does this: AddHandler watermarked .jpg AddHandler watermarked .jpeg AddHandler watermarked .gif AddHandler watermarked .png However, the website I'm working on has a partners page in which the watermarks shouldn't appear. Every other image in the uploads directory needs to have the watermark, and that's working fine. What I'd like to do is something like this: if(!$isPartnerPage){ AddHandler watermarked .jpg AddHandler watermarked .jpeg AddHandler watermarked .gif AddHandler watermarked .png } But obviously .htaccess isn't PHP. Could anyone suggest a good way of doing what I'm trying to do? The alternative would be to have those particular files that shouldn't be watermarked not uploaded into the uploads directory, but that might make things more complicated and confusing. Thanks for any help!
|
|
# ? Feb 19, 2012 23:47 |
|
every posted:This is more of an htaccess / apache question, but I couldn't find a more appropriate thread, so here goes: Make some watermarked images. Make some not-watermarked images. Put the watermarked images everywhere you need them and put the not-watermarked images where you need them. Add a rewrite rule to deny on the not-watermarked images for an invalid referrer (basically any referrer but the partners page) and you're done.
|
# ? Feb 20, 2012 02:48 |
|
Anyone know of a way of sorting a multidimensional array by one of the sub-values? I'd prefer keeping the root keys intact(assoc) but rearrange the actual order of the array. I figure I can come up with a way to do it myself but there might be an existing function that can do it.
|
# ? Feb 29, 2012 12:37 |
|
revmoo posted:Anyone know of a way of sorting a multidimensional array by one of the sub-values? I'd prefer keeping the root keys intact(assoc) but rearrange the actual order of the array. I figure I can come up with a way to do it myself but there might be an existing function that can do it. I could be wrong (PHP has a staggering number of functions for manipulating arrays, with little in the way of naming conventions) but I don't think there is a core library function to do this. But it is easy enough to make it happen using uasort(). (You need uasort() rather than usort() because you want to preserve the array keys.) Suppose you have an array $myArray of arrays and each of the sub-arrays has a key 'myKey' whose value is an integer, and you want to sort the sub-arrays into increasing order based on the values under 'myKey'. You can do this like so: code:
|
# ? Feb 29, 2012 16:37 |
|
How do $x and $y get passed into that? Also I think that would be a prime spot to use an anonymous function, did you write it without for to be clear or some other reason? I ended up solving the problem by rethinking it. I assigned a weight subkey and increment/decrement the weight with a few functions and then sort the entire thing by enumerating the weights into an array and matching up keys in a big nested loop that re-sorts the final multi-d array by the weight values. Works, but I've probably reinvented some sorting algorithm.
|
# ? Mar 1, 2012 04:35 |
|
revmoo posted:How do $x and $y get passed into that? Also I think that would be a prime spot to use an anonymous function, did you write it without for to be clear or some other reason? uasort() expects its second argument to be a function usable for sorting. It expects the function to be one that takes two parameters, and returns the values -1, 1 and 0 (hopefully in a consistent way). uasort() takes care of actually calling the function. It does so internally, passing in the array elements of the array to be sorted as the $x and $y values. The user does not need to tell it how to do this, just supply a sorting function that behaves in the appropriate way. I don't recall which version of PHP you have to be running in order to be able to use anonymous functions, so to be clear of that consideration I didn't bother.
|
# ? Mar 1, 2012 14:51 |
|
Really good info, thanks!
|
# ? Mar 2, 2012 03:08 |
|
PHP5.4 has been released: http://www.php.net/archive/2012.php#id2012-03-01-1 Has a lot of great features that make it more like a "real" language. I know I'll use the poo poo out of short arrays, easier array dereferencing, and being able to call methods on a newly build class: php:<? $stuff = (new Something)->doStuff();?>
|
# ? Mar 2, 2012 10:49 |
|
|
# ? Jun 4, 2024 16:22 |
|
musclecoder posted:PHP5.4 has been released: http://www.php.net/archive/2012.php#id2012-03-01-1
|
# ? Mar 2, 2012 16:03 |