![]() ![]() Therefore, using !isset() is not the same as using empty(), nor is “!empty” the same as using isset(). They actually check for two very different things. There are two main points in this article:ġ) isset() and empty() are not exactly opposite of each other. Just a quick tip for people struggling with this. On the other hand, if you are trying to check the value of a radio button or combobox in which one of your possible values is “0”, then you should be using the isset() function.įor the first few form processors I worked on, I was consistently using the following code: if(isset($_POST) & $_POST != "")Ī more efficient way of doing this is obviously to use: if(!empty($_POST)) Therefore, if you are trying to process a form, and you want to make sure the person entered something into the field, you are much better off checking to see if the form value is empty, than you are checking to see if it isset(). Something else you need to know is that textareas and textboxes in forms will be sent with “” values rather than NULL values to the $_POST array. All variables that have any type of value, whether it is 0, a blank text string, etc. In other words, only variables that don’t exist (or, variables with strictly NULL values) will return FALSE on the isset() function. Returns TRUE if var exists FALSE otherwise. Here is the description of what the isset() function returns: In other words, everything from NULL, to 0 to “” will return TRUE when using the empty() function. Returns FALSE if var has a non-empty and non-zero value. I learned this the hard way a long time ago while I was working on my first few form processors in PHP.įrom the PHP Web site, referring to the empty() function: Stick with it though! It might not make sense now but you will have that moment where it all starts to come together.This is just a quick tutorial regarding the empty() and isset() functions for people that are fairly new to the world of PHP programming. I'm new to answering on the forums so sorry that I didn't include some pictures as reference. If it returns false then it will move down to it's else block and execute the code inside it, which is just echoing out the message that it's an invalid shirt id. ![]() Same thing applies to the second if statement. If it returns false it then moves to the else block of the first if statement and then echos out the message that there is no shirt id. So in the first if statement it would check to see if there was even an id to begin with. The else statements would be triggered if the if isset function returned false rather then true. After that it will then echo out a message saying that it's a valid shirt id. If that is set and returns true just like in the previous if statement, it will assign the product id of the products array to a variable called $product. Then it checks to see if the products id of the products array is set. After it has done that it continues down the script to the next if statement. If it returns true and it is indeed set then it assigns the id to the $product_id variable. In the first part of the block of code it's getting the id. Basically it will return true if the variable exists. If the variable is set to something other then null or false then it will return true. So the isset function checks wether the variable is set or null. Basically where you think that you have a variable that is (1) hard to remember, (2) long to type out, or (3) will be frequently used. Use it where you feel it's necessary to use a local variable. ![]() This practise is almost always just to make your life easier, and doesn't have an impact on the actual program. There is absolutely nothing wrong with using $products if you'd prefer, but it's both easier to type and remember and less likely to miss-type if you use a shorter variable of your choice. Same thing goes for $product = $products. But I think that increases the chances of error. If you don't feel comfortable doing it this way, or if you feel it's redundant, then that's perfectly fine and acceptable too. It makes it easier to read and easier to see the known's and unknown's of the problem and thus easier to solve it if everything is written down in one place rather than having the mess of papers laying everywhere. I think of it as the same reason you would want to first copy a math problem onto your sheet in a test before solving it. well it's just "cleaner" for you as a programmer to just use a local variable. Yes you can use $_GET if you'd like - there is nothing wrong about that.īut I'd say it's a good practise not to use it though because. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |