Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP variables / arrays

Featured Replies

I have a php script with an integer variable that can equal 1 - 20. I want the variable to then change to a text string, depending on the number it was. I'm sure it has something to do with arrays, but have no real clue about them yet. Does anybody have any ideas?

 

e.g 1 is cat, 2 is dog, 3 is snail.

 

so if the variable was 1, it would be change to equal 'cat'.

 

Thanks in advance.

I have a php script with an integer variable that can equal 1 - 20. I want the variable to then change to a text string, depending on the number it was. I'm sure it has something to do with arrays, but have no real clue about them yet. Does anybody have any ideas?

 

e.g 1 is cat, 2 is dog, 3 is snail.

 

so if the variable was 1, it would be change to equal 'cat'.

 

Thanks in advance.

My link

 

Have a read through that and that will/should answer your question :)

 

Ben

You could try a switch:

 

switch($integer)
{
case 1:
$animal="cat";
break;

case 2:
$animal="dog"
break;
//and so on

default:
$animal="tapir";
}

A switch is a bad method to use. Just use an associative array

$array = (
0 => 'cat',
1 => 'dog',
2 => 'owl',
3 => 'pig',
4 => 'cow'
);

echo $array[3]; // echos pig

  • Author

Yeah I went for an array in the end, it seems to be working so far!

 

I went for something a bit like this:

$string = 1; //some integer
$array=array(1=>"Cat",2=>"Dog",3=>"Rabbit",4=>"Mouse");
$string = $array[$string]; //changed to text string

 

It's alright practice to put a variable in the [] right?

 

lh

It's alright practice to put a variable in the [] right?

Certainly is - otherwise iterating over the contents of an array would be rather tricky! The "traditional" way to do iteration uses a counter with a loop:

 

$arr_size = count($array);
for($i=0; $i<$arr_size; $i++) {
 $str = $array[$i];
}

That's just one example which uses a variable index - not necessarily the clearest way to iterate over the array, but it makes its point!

  • Author

Certainly is - otherwise iterating over the contents of an array would be rather tricky! The "traditional" way to do iteration uses a counter with a loop:

$arr_size = count($array);for($i=0; $i<$arr_size; $i++) {  $str = $array[$i];}

That's just one example which uses a variable index - not necessarily the clearest way to iterate over the array, but it makes its point!

 

Hey, thanks a lot! I'm slowly finding my feet with this PHP stuff :)

Just a quick note. With your original array you show

$array=array(1=>"Cat",2=>"Dog",3=>"Rabbit",4=>"Mouse");

Since arrays work off numbers by default (incrementing by 1 each time) you could just use

$array=array(1=>"Cat","Dog","Rabbit""Mouse");

 

and Dog, Rabbit and Mouse will automatically have the values 2, 3 and 4 respectively

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.