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.

Need little help with PHP

Featured Replies

Hello :)

I need to do the following and i need some help :

these are the variable i have :

 

 

$Variable1 = 09

$Variable2 = 06

$Variable3 = 12

$Variable4 = 16

 

I need to know how to print the content of these variables in an Incremental way

like this :

 

06

09

12

16

 

Thank you in advance

  • Author

How about

echo $Variable1 . '<br />' . $Variable2 . '<br />' . $Variable3 . '<br />' . $Variable4 . '<br />' ; 

The result to that would be :

09

06

12

16

and i want :

06

09

12

16

in brief i want PHP to check and classify the variables from the minimum to to the maximum number

that way it will echo the smallest number first, then the second, then the third and so on ... and i don't want to do that manually

you can try something like this

 


<?php
$array = array();
// you can change the numbers for variables i.e.
//$array[] = $Variable1;
$array[] = 9;
$array[] = 6;
$array[] = 12;
$array[] = 16;
array_multisort($array);

echo $array[0].'<br />';
echo $array[1].'<br />';
echo $array[2].'<br />';
echo $array[3].'<br />';

?>

Edited by D4Y0

And if you use array, how about using loop?

Both are the methode to print this one is loop or second is through arry.

 

Would that be 'arry from Sham69's great hit?

 

Here you go:

$vars = array("09","06","12","16");

sort($vars);

foreach ($vars as $value)
{
echo $value."<br />";
}

 

You can always convert them back to integer if you need them as integer. I put them as string to maintain the leading zero in the first 2 array elements.

 

Hope this helps.

 

Regards,

 

L2c.

Edited by Love2c0de

If you have integers instead of strings here is my advice:

$Variables = array(9,6,12,26,112);
sort($Variables);
foreach ( $Variables as $v ){
 echo str_pad($v, 2, 0, STR_PAD_LEFT)."<br />";
}

 

The str_pad() function fills the zeros for you where you need, and if you want you can increase the number of zeros by increasing the second parameter of the function. (http://php.net/manual/en/function.str-pad.php)

Thanks for that. Always wondered how to work with integers but to keep the leading zeros.

 

Great post.

 

Kind regards,

 

L2c.

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.