Jump to content

Recommended Posts

Hi!

I need to have this written by the use of an array, a function, and a loop.

 

"The number 1 state alphabetically is: Alabama

The number 2 state alphabetically is: Alaska

The number 3 state alphabetically is: Arizona

The number 4 state alphabetically is: Arkansas

The number 5 state alphabetically is: California

The number 6 state alphabetically is: Colorado

The number 7 state alphabetically is: Connecticut

The number 8 state alphabetically is: Delaware

The number 9 state alphabetically is: Florida

The number 10 state alphabetically is: Georgia

The number 11 state alphabetically is: Hawaii"

 

I'm just learning php so bear with me. This is what I got so far:

 

<?php

 

$state[1]="Alabama";

$state[2]="Alaska";

$state[3]="Arizona";

$state[4]="Arkansas";

$state[5]="California";

$state[6]="Colorado";

$state[7]="Connecticut";

$state[8]="Delaware";

$state[9]="Florida";

$state[10]="Georgia";

$state[11]="Hawaii";

$state[12]="Idaho";

 

function findState ($snumber, $sname)

{

echo $snumber. "1" . $state . "Alabama" . "<br/>";

}

 

echo findState ("The number", "state alphabetically is: ");

?>

 

If anyone could be so kind and give me hand would be awesome.

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/182575-arrays-functions-and-loop-help/
Share on other sites

Well first things first, $state is not in the scope of the function, you either need to make that a global or add it inside the function tags or else you cannot access it.

 

Number 2, you never call an index of the state array and you never use sname, which I am not sure what it is suppose to do.

 

Number 3, your findstate function does not give a number as the first argument it gives a string and the second argument seems to give nothing either (unless you just wrote those in there for an example).

 

If you need help I would read up on variable scope and array's as they both will help you and once you get the basic function working with static values, you can then look into use a for loop to loop through and display the output like you want.

 

The reason for me not giving any code is this seems like homework and you will not learn if I did it for you :)

$state[1]="Alabama";

$state[2]="Alaska";

$state[3]="Arizona";

$state[4]="Arkansas";

$state[5]="California";

$state[6]="Colorado";

$state[7]="Connecticut";

$state[8]="Delaware";

$state[9]="Florida";

$state[10]="Georgia";

$state[11]="Hawaii";

$state[12]="Idaho";

 

function findState($state[], $number)

      echo $state[$number];

}

 

Thanks for responding, premiso

I've been reading php for days. My eyes hurt.

What I need to do is have the numbers and states changed while everything remains the same.

So should I just write $state in the the scope of the function.

What do I call an index?

Can you point me in a good direction?

I've been reading w3schools and they're very vague.

Also my time is running out...

foreach may help you out, but if you are required to use a function as well as a loop, you need to revise your function and incorporate a for loop.

 

The gist of arrays is they are access by the index, so $state[1] will pull Alabama out. So given that you can use a variable such as $i which is commonly used in a for loop as the counter to pull $state[$i] which will pull out whatever item is at index $i.

 

Hopefully that helps. Are you taking this class to learn programming? Or just taking cause your major requires a Computer Science class?

Thanks dbillings

I think I've tried that too before but it doesn't allow the $state[] in the scope of the function it says "Parse error: syntax error, unexpected '[', expecting ')'"

But thanks anyway

 

 

$state[1]="Alabama";

$state[2]="Alaska";

$state[3]="Arizona";

$state[4]="Arkansas";

$state[5]="California";

$state[6]="Colorado";

$state[7]="Connecticut";

$state[8]="Delaware";

$state[9]="Florida";

$state[10]="Georgia";

$state[11]="Hawaii";

$state[12]="Idaho";

 

function findState($state[], $number)

      echo $state[$number];

}

 

 

premisso, the thing is that i can't seem to figure out hot to get the number (1) and state name (Alabama) to change with the words (The number) and (state alphabetically is:) in between. I can only get them if their following one another.

I need the function to write "The number 1 state alphabetically is: Alabama"

So that I can start working on the for loop and get

"The number 2 state alphabetically is: Alaska" and so forth.

Am I making myself clear?

Yes I'm taking this class as an elective.

I appreciate you helping me!

 

Just to fix his code:

 

function findState($number, $state=array())
      echo $state[$number];
}

// pull index 1 out of state array.
echo findState(1, $state);

foreach is the key:

 

$state[1]="Alabama";
$state[2]="Alaska";
$state[3]="Arizona";
$state[4]="Arkansas";
$state[5]="California";
$state[6]="Colorado";
$state[7]="Connecticut";
$state[8]="Delaware";
$state[9]="Florida";
$state[10]="Georgia";
$state[11]="Hawaii";
$state[12]="Idaho";

foreach ($state as $num => $stateName) {
    echo "The number $num state alphabetically is: $stateName<br />";
}

 

Basically that loops through the array $state and goes through each element and assigns the $num equal to the current number and $statename equal to the current value located at index of the current number.

That's awesome. You're good at this. The result is exactly this one. However, I need to incorporate a function and have the for loop calling it. Otherwise the loop goes directly to the array, isn't that so?

Can't thank you enough.

 

 

foreach is the key:

 

$state[1]="Alabama";
$state[2]="Alaska";
$state[3]="Arizona";
$state[4]="Arkansas";
$state[5]="California";
$state[6]="Colorado";
$state[7]="Connecticut";
$state[8]="Delaware";
$state[9]="Florida";
$state[10]="Georgia";
$state[11]="Hawaii";
$state[12]="Idaho";

foreach ($state as $num => $stateName) {
    echo "The number $num state alphabetically is: $stateName<br />";
}

 

Basically that loops through the array $state and goes through each element and assigns the $num equal to the current number and $statename equal to the current value located at index of the current number.

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.