Jump to content

[SOLVED] >>>How can I search an Array?<<<


lotrfan

Recommended Posts

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.