lotrfan Posted September 30, 2007 Share Posted September 30, 2007 Hi all, I started PHP about a month ago. Loving it. Anyway, I am trying to use an code with this: $_REQUEST[angle_a]=$angle_a; $_REQUEST[angle_b]=$angle_b; $_REQUEST[angle_c]=$angle_c; and this... $angle[0]=$angle_a; $angle[1]=$angle_b; $angle[2]=$angle_c; I want this to be an array so that the PHP can see which angle (in the triangle) is in a certain place. I want now to be able to find out if no value has been given for these angles. Because if no value for an angle has been given by the user in an input form, the code will then proceed to find the remaining angles. So how do I "search" this array to find which $angle[?] has no value? Thanks. PS -- Please use layman's terms, I'm not very good at PHP yet... Quote Link to comment Share on other sites More sharing options...
Orio Posted September 30, 2007 Share Posted September 30, 2007 Ok, first of all you are doing it in the wrong direction: <?php //WRONG $_REQUEST[angle_a]=$angle_a; $_REQUEST[angle_b]=$angle_b; $_REQUEST[angle_c]=$angle_c; //RIGHT $angle_a=$_REQUEST[angle_a]; $angle_b=$_REQUEST[angle_b]; $angle_c=$_REQUEST[angle_c]; ?> You want to set the $angle_* variables, not the $_REQUEST[ * ] variables I remember I was making these mistakes once too. Now, as for checking if a value is empty, you simply use the empty() function. This function returns TRUE if the var is empty, FALSE if it is not. So if you want to check if $var is empty you'd do something like this: <?php if(empty($var)) { echo "it's empty!"; }else{ echo "it's not!"; } ?> Feel free to ask more questions Orio. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Share Posted September 30, 2007 <?php $array= array("this","is","cool"); if (in_array($array,"this",TRUE)) { echo "This is an array"; }else { echo "this is not an array";}?> Quote Link to comment Share on other sites More sharing options...
lotrfan Posted September 30, 2007 Author Share Posted September 30, 2007 Thanks for the quick reply -- it really means a lot! You obviously know what you are talking about. I fixed the transposition of the $_REQUEST as well. Helped a lot . Anyway, here's what is did: if (empty($angle[0])){ echo "No value for angle0 given.";} if (empty($angle[1])){ echo "No value for angle1 given.";} if (empty($angle[2])){ echo "No value for angle2 given.";} This works, but is there a way that I wouldn't have to write it out three times? Could I somehow tell PHP to look for ALL keys given within the $angle[] array? Thanks! Quote Link to comment Share on other sites More sharing options...
Orio Posted September 30, 2007 Share Posted September 30, 2007 Use a loop: <?php for($i=0; $i<=2; $i++) { if (empty($angle[$i])){ echo "No value for angle $i given.";} } ?> This way you can have as many angles as you want, not only for triangles Orio. Quote Link to comment Share on other sites More sharing options...
lotrfan Posted September 30, 2007 Author Share Posted September 30, 2007 Neat. That would make things a lot easier for a decagon! Now what if I wanted to identify the angles I DO have? Would I use the 'is_array' function? Is that the opposite of the 'empty'? How would I do that? I am trying to use the $number_of_angles variable to tell user how many angles there are. I just tried to use an else statement after the if statement, but that didn't work. This is what I'm trying to do: $number_of_angles=0; for($i=0; $i<=2; $i++) { if (empty($angle[$i])){ echo "No value for angle $i given."; else ($number_of_angles= $number_of_angles + 1)} } echo "There are " ,$number_of_angles , " angles in this triangle."; Thanks once again. If I am wasting your life away with my questions, please tell me -- I don't want to be an irritant. Quote Link to comment Share on other sites More sharing options...
Orio Posted September 30, 2007 Share Posted September 30, 2007 More like this <?php $number_of_angles=0; for($i=0; $i<=2; $i++) { if (empty($angle[$i])){ echo "No value for angle $i given."; }else{ $number_of_angles= $number_of_angles + 1; } } echo "There are " .$number_of_angles ." angles in this triangle."; ?> Orio. Quote Link to comment Share on other sites More sharing options...
lotrfan Posted September 30, 2007 Author Share Posted September 30, 2007 Thanks again Orio. You are very kind so spend so much time helping others that need it. Problem SOLVED. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.