|
Having coded in PHP with little abstract knowledge of language standards, I'm guessing what y'all are saying is, if it returns an integer, it should always return an integer rather than switching to a boolean? Or throw an exception, which as we know PHP just doesn't do for its internal functions.
|
# ? Jul 20, 2012 14:23 |
|
|
# ? Jun 6, 2024 22:57 |
|
It could gently caress up for anyone who doesn't know about PHP's quirks:PHP code:
|
# ? Jul 20, 2012 14:40 |
|
Golbez posted:Having coded in PHP with little abstract knowledge of language standards, I'm guessing what y'all are saying is, if it returns an integer, it should always return an integer rather than switching to a boolean? Or throw an exception, which as we know PHP just doesn't do for its internal functions. Ideally yes; from a logical point of view, a function should always return a value of a single type. (because logically, mathematically and "properly" a function maps a domain onto a co-domain, and those "should" be defined sets, and "integer and also sometimes FALSE" isn't a sensible set definition). But you're right; PHP does not do this. (and its far from alone in that behaviour) Its actually "quite consistent" in that PHP returns "Value or FALSE" all over the place. (rest of comment is not a direct reply to any particular post!) As Optimus Prime Ribs points out, and Scaramouche fell into the trap of, that makes this the ONLY safe construct in such cases: php:<? $x = "butt"; $y = "my butt"; // construct for test "is string x in string y?" if(strpos($y, $x) !== FALSE) { // butt you say? } ?> Its only fair to note that PHP is absolutely NOT AMBIGUOUS about this; http://php.net/manual/en/function.strpos.php posted:Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1. And that is followed by a big red box with a warning sign, a title of "Warning" and bold text warning you about sometimes-boolean returns, and how to handle them properly. On the one hand, yep; mixed return types are bad. On the other hand, the problem disappears if you read the loving manual! Edit: Basically if you're a lazy coder regarding types, then PHP will let you skip along happily for quite a while, before holding you down and publicly doing horrible things to you (by which I mean by breaking your code in a seemingly obscure and certainly humiliating way). On the other hand a strict typed language like C will simply shout endlessly "you suck! gently caress off!" before deigning to run any of your code at all. But on the plus side; its doing that in the privacy of your own home, not 6 months after you published the code to the world! The common defense, of course, is to not be lazy regarding types. KuruMonkey fucked around with this message at 15:39 on Jul 20, 2012 |
# ? Jul 20, 2012 15:33 |
|
KuruMonkey posted:Ideally yes; from a logical point of view, a function should always return a value of a single type. (because logically, mathematically and "properly" a function maps a domain onto a co-domain, and those "should" be defined sets, and "integer and also sometimes FALSE" isn't a sensible set definition). Why not? If we assume non-integer mathematical objects "true" and "false", then {integers} union {false} seems like a perfectly well-defined set (in the mathematical sense) to me. It is the set of integers, augmented with an extra element that is not an integer. Perhaps it would be a good idea to define('NOT_FOUND', false), because then you can do if (search_function(...) === NOT_FOUND) { ... and the intention is clear. Of course, this does not solve the problem that you have to check for === equality. You could define a function not_found() to do it for you, that returns true if passed false, false if passed an integer, and throws an exception or whatever otherwise. Then you could do if (not_found(search_function(...))) { ..., at the cost of some overhead. ------------------- I recall that "reset.css" files, to ensure that Internet Explorer displays pages similarly to other browsers, are or were popular. I wonder why nobody has made a "reset.php" to try to streamline some of the more visible oddities of PHP. By this I mean you could write a PHP script that for the most part just defines a set of functions as wrappers around the PHP builtins, but with a consistent naming scheme, argument order and so on and so forth, and with an attempt to logically collect together similar pieces of functionality rather than having eighteen hundred billion billion slightly different array functions. You could even have the search functions return -1 on not found, if you wanted. Obviously this would be inefficient (because it would add the step of recompiling this reset file to every single page request, and because calling user-defined wrapper functions adds some overhead to your script). But it seems like it could smooth the user experience to some small degree.
|
# ? Jul 20, 2012 17:21 |
|
I realize that a lot of it is my own expectation about what something should return; the fact that the function returns differing types based on the input is a bit strange but not 'OMG CODING HORROR'. I think what bothers me more is the loosy goosy nature of the comparisons.
|
# ? Jul 20, 2012 18:37 |
|
Scaramouche posted:I realize that a lot of it is my own expectation about what something should return; the fact that the function returns differing types based on the input is a bit strange but not 'OMG CODING HORROR'. I think what bothers me more is the loosy goosy nature of the comparisons. Yeah, lots of languages do the -1/false response that will equate to a loose type match that is the same. Javascript has the same issue. It's not the method returning this that is the issue so much (the alternative is to throw an exception?), but the language that enables this to be an issue. As long as you know about it, and can read the giant warning on php.net, you should be good.
|
# ? Jul 20, 2012 19:30 |
|
Scaramouche posted:I realize that a lot of it is my own expectation about what something should return; the fact that the function returns differing types based on the input is a bit strange but not 'OMG CODING HORROR'. I think what bothers me more is the loosy goosy nature of the comparisons. I agree completely - I think that's why I was a bit confused about what you were saying. Even though the return type is mixed, I think it's okay in this instance because it does make sense to return a FALSE. The problem lies in the fact that 0 will still compare as false, I guess it pays to always use the strict comparisons - sucks for people just getting into the language though as it's a frequent gotcha that will continuously crop up.
|
# ? Jul 23, 2012 00:55 |
|
When learning PHP it confused the hell out of me that 0, false, "" and null all mean the same thing if you're comparing them with each other using == because I failed to read the manual. I realised this because of strpos, too, which I'm sure a lot of people did. And also: "banana" == 0 "banana" != false "banana" != null "banana" ! "banana" !== 0 This caught me out once and I didn't think it was obvious in the documentation. A string that doesn't have a decimal point, an e or an E in it == 0, but obviously !== 0 since it's a different type. Easy to make a mistake on this.
|
# ? Jul 23, 2012 17:00 |
|
President Anime 2008 posted:When learning PHP it confused the hell out of me that 0, false, "" and null all mean the same thing if you're comparing them with each other using == because I failed to read the manual. I realised this because of strpos, too, which I'm sure a lot of people did. And also: The best part is that "==" is not transitive. quote:"php" == 0 => true
|
# ? Jul 23, 2012 17:16 |
|
I'm almost done transitioning my hobby project to PDO after the announcement that mysql_ queries are being depreciated. I'm having one problem with a certain query, though. Basically I'm keeping track of the number of generated items by doing code:
I'd really like to keep it like this to reduce the number of SQL queries, but if PDO doesn't want to cooperate, I'm open to suggestions. Here's the specific code block I'm talking about for reference: php:<? $total_gen_update = $db->prepare("UPDATE stats SET total_generated=total_generated+:totalgen"); $total_gen_update->bindValue(':totalgen', $stats_generated, PDO::PARAM_INT); $total_gen_update->execute(); ?>
|
# ? Jul 24, 2012 03:06 |
|
What, exactly, is going wrong? It's been a while since I used named placeholders, but that looks perfectly correct...
|
# ? Jul 24, 2012 04:56 |
|
When I run the page it doesn't submit the updated count. I'll check over it again to make sure I didn't make some typo, though.
|
# ? Jul 24, 2012 13:12 |
|
Doesn't that query need a WHERE clause? Also, PDO provides error reporting. you can add something like this: php:<? $total_gen_update = $db->prepare("UPDATE stats SET total_generated=total_generated+:totalgen"); $total_gen_update->bindValue(':totalgen', $stats_generated, PDO::PARAM_INT); $r = $total_gen_update->execute(); if( $r === false ) { print_r( $total_gen_update->errorInfo() ); } ?>
|
# ? Jul 24, 2012 21:41 |
|
Unfortunately I can't get XAMPP running on my machine at the moment, but a group mate is having some issues with this project we're working on. I downloaded some SQL script that creates a table for zipcodes, and it has a text file which the SQL file tries to perform a bulk insert from. When he tries to run the bulk insert command in phpmyadmin, it fails. Is this because it's trying to import from C:\zip.txt? To me, I don't think it would be able to access a local file like that, but I have no idea. Also the table the SQL script creates are all varchars, but none of the values in the text file have quotes around them. Could that be an issue?
|
# ? Jul 26, 2012 02:20 |
It's probably doing a LOAD DATA INFILE. I'm pretty sure phpMyAdmin has a way to "import" csv files where you can upload the csv and then set the LOAD DATA INFILE parameters, I would just use that.
|
|
# ? Jul 26, 2012 02:34 |
|
This sounds like the MaxMind geoip database, is it? If so this might help: http://bartomedia.blogspot.ca/2007/11/maxmind-geoip-setup-tutorial-using.html
|
# ? Jul 26, 2012 20:47 |
|
Sab669 posted:Unfortunately I can't get XAMPP running on my machine at the moment, but a group mate is having some issues with this project we're working on. Sab you need to ensure that you have told phpmyadmin which character denotes a new line. You could also load the txt file into excel or your favourite spreadsheet maker and save it as a csv. OR INSERT INTO yourttable (columnname) VALUES (Value1), (Value2), (Value3)
|
# ? Jul 27, 2012 02:55 |
|
It was just a thing I downloaded off Google, can't seem to find it anymore but I know the script included the line delimiter. If he's still having an issue when I see him next week however, yea, I'll probably just throw it into excel and CSV it.
|
# ? Jul 27, 2012 13:52 |
|
I'm using json_decode on a json file that contains both empty values and null values, does anyone know why php would read both of them as null? Running the empty() function on both of them returns true and if I do if($variable == null) they're both match the condition.
|
# ? Jul 29, 2012 23:40 |
|
emoltra posted:I'm using json_decode on a json file that contains both empty values and null values, does anyone know why php would read both of them as null? It's because PHP. Try to see if isset() works for you.
|
# ? Jul 29, 2012 23:50 |
|
The documentation for empty clearly states that null and "" are deemed to be empty. Why do you have values that way to begin with? Try isset.
|
# ? Jul 29, 2012 23:51 |
|
Mister Chief posted:The documentation for empty clearly states that null and "" are deemed to be empty. Why do you have values that way to begin with? Null and the empty string are semantically different things.
|
# ? Jul 30, 2012 01:38 |
|
emoltra posted:Running the empty() function on both of them returns true and if I do if($variable == null) they're both match the condition. == is a similarity construct, and as has been noted by others '' == null. You probably want to use either === for identity here or to use is_null()
|
# ? Jul 30, 2012 03:47 |
|
This is a bit of a stretch, but does anyone have experience with multi-domain authentication against AD-LDAP? The forest shares a common root domain so I can hit all of them with the global catalog. The problem we're running into is we're trying to use the supplied user credentials to authenticate, however because we don't know what domain the user is in we can't just append it (@blah.blah.com) to the user value. I could use a ldap service account is look up the UPN from the username then use it with the supplied credentials to authenticate but that seems a bit messy and I suspect there would be some lag.
|
# ? Jul 31, 2012 00:43 |
|
Ashex posted:This is a bit of a stretch, but does anyone have experience with multi-domain authentication against AD-LDAP? The forest shares a common root domain so I can hit all of them with the global catalog. If you're in a multi-domain environment, why not just present the user with a select box to choose the domain their user account is part of? I've seen this method used many times even for AD integrated services and portals.
|
# ? Jul 31, 2012 04:27 |
|
I'm going to be implementing user authentication for the first time and while I could write my own, I really don't want to. What are some of the more popular user auth libraries out there?
|
# ? Jul 31, 2012 13:02 |
|
IT Guy posted:I'm going to be implementing user authentication for the first time and while I could write my own, I really don't want to. What are some of the more popular user auth libraries out there? Well, to whore out my own product a bit, you could use my API: https://accthub.com. It's for storing user accounts, addresses, and performing authentication against them. It's geared towards mobile developers, but there's really no reason why you couldn't use it for a web app (ironically, it was originally built for web apps). As for actual libraries you can install yourself, sorry, can't help you there.
|
# ? Jul 31, 2012 14:14 |
|
musclecoder posted:Well, to whore out my own product a bit, you could use my API: https://accthub.com. It's for storing user accounts, addresses, and performing authentication against them. It's geared towards mobile developers, but there's really no reason why you couldn't use it for a web app (ironically, it was originally built for web apps). I appreciate your opinion. However, I'm looking for something tried and true. Your product looks to be in a beta which I'm hesitant about. In addition, I want to store user's credentials in my own database (from what I see your product means I'd be storing stuff in the cloud?), preferably with some type of bcrypt hash. Sorry, I'm sure your product is fine and I don't mean to offend you, I'm just looking for something I can implement into my current database.
|
# ? Jul 31, 2012 15:40 |
|
IT Guy posted:I appreciate your opinion. However, I'm looking for something tried and true. Your product looks to be in a beta which I'm hesitant about. In addition, I want to store user's credentials in my own database (from what I see your product means I'd be storing stuff in the cloud?), preferably with some type of bcrypt hash. Thanks for checking it out and completely understood. Just thought I'd throw it out there. If you do find a solution, let me know. I haven't seen any open source auth system that uses bcrypt.
|
# ? Aug 1, 2012 15:11 |
|
musclecoder posted:Thanks for checking it out and completely understood. Just thought I'd throw it out there. I ended up just writing my own and storing passwords using a per application (not per user) salt and SHA256 hash. My site is an internal web application that isn't available to the WAN so it should be fine for our needs.
|
# ? Aug 1, 2012 15:39 |
|
IT Guy posted:I ended up just writing my own and storing passwords using a per application (not per user) salt and SHA256 hash. My site is an internal web application that isn't available to the WAN so it should be fine for our needs. Check out PHPass. It uses bcrypt and is used by a lot of major CMSs to hash their passwords.
|
# ? Aug 1, 2012 23:24 |
|
Just be sure to kick it out of "compatible" mode, during which it falls back to md5.
|
# ? Aug 2, 2012 01:05 |
|
Mister Chief posted:Check out PHPass. Awesome, looks like what I was after, thanks.
|
# ? Aug 2, 2012 01:10 |
|
I think choosing PHP for our language of choice for my senior project was not a good idea. I am not strong enough to carry this group
|
# ? Aug 2, 2012 02:59 |
|
I'm having some trouble with unicode-related nonsense in a PHP project I'm working on. I have a mysql database storing a bunch of stuff, including unicode strings. Using phpmyadmin or the command line mysql utility display these strings correctly. When I try to pull them from the database to display on a page, all of the non-ascii characters are replaced with �. I've tried making sure PHP is set to use a unicode character set and is actually sending utf-8 by putting this at the beginning of the page: code:
Thanks for any assistance Rahu fucked around with this message at 06:57 on Aug 2, 2012 |
# ? Aug 2, 2012 06:53 |
|
MySQL? Chances are that you need to set the database connection character set to match that of the data in the tables. Also double-check that the character set on the tables and columns matches what you expect. If that still isn't doing the trick, echo the string using urlencode. This'll give you the actual bytes that you're pulling down from the database, which you can compare to what the expected unicode code points should be.
|
# ? Aug 2, 2012 07:15 |
|
You could try something like htmlspecialchars. http://php.net/manual/en/function.htmlspecialchars.php
|
# ? Aug 2, 2012 07:19 |
|
McGlockenshire posted:MySQL? Chances are that you need to set the database connection character set to match that of the data in the tables. Also double-check that the character set on the tables and columns matches what you expect. That was it, thanks. I never considered the character set of the connection Rahu fucked around with this message at 07:31 on Aug 2, 2012 |
# ? Aug 2, 2012 07:29 |
|
Mister Chief posted:Check out PHPass. It uses bcrypt and is used by a lot of major CMSs to hash their passwords. PHP 5.3 supports bcrypt natively with the crypt() method. I'd use that instead and wrap it around a small class. PHPass is nice, but it's some very old PHP4 code.
|
# ? Aug 2, 2012 10:22 |
|
|
# ? Jun 6, 2024 22:57 |
|
Here's a tutorial which demonstrates how to do the same things that PHPass does except with the new crypt functions.
|
# ? Aug 2, 2012 10:47 |