This question already has answers here:
Generate random ENUM value [PHP, MySQL]
(2 answers)
Getting values for an enum?
(2 answers)
Get random item from array [duplicate]
(4 answers)
How to get random value out of an array?
(21 answers)
Closed 5 months ago.
This is my enum class. I want a random value every time from this enum class. I am using the PHP 8.1 enum feature.
<?php>
declare(strict_types=1);
namespace App\Enums;
enum Division: string
{
case BARISAL = 'barisal';
case CHITTAGONG = 'chittagong';
case DHAKA = 'dhaka';
case KHULNA = 'khulna';
case RAJSHAHI = 'rajshahi';
case RANGPUR = 'rangpur';
case MYMENSINGH = 'mymensing';
case SYLHET = 'sylhet';
}
<?php>
Related
This question already has answers here:
How can I add commas to numbers in PHP
(6 answers)
MySQL - Thousands separator
(1 answer)
Closed 3 years ago.
I am using PHP to count rows from the SQL DB. Now i want to format the counts with a thousandsseperator.
The current code looks like this:
public function countFollowers(){
if (empty($this->user_id) || !is_numeric($this->user_id)) {
return false;
}
$user_id = $this->user_id;
$t_conn = T_CONNECTIV;
self::$db->where('following_id',$user_id)->where('type',1);
return self::$db->getValue($t_conn,"COUNT(`id`)");
}
And on the html side with this:
<?php echo $context['user_followers']; ?>
The current output looks like this 2340 !
How can i format this output like this 2.340 ? With a point as thousandseperator?
This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 6 years ago.
Using PHP variable variables with mysqli_fetch_assoc to auto-format variables like $column_name="some_value" (code below):
while ($account_row=mysqli_fetch_assoc($account_results))
{
foreach ($account_row as $key=>$value)
{
$$key=trim(stripslashes($value));
}
}
So if I have a column "username" and row value "someuser", this code creates:
$username="someuser";
However, in some cases I need variable names to be DIFFERENT from column names. For example, I need code to create:
$username_temp="someuser";
How could I do that? Using this code gives an error:
$$key."_temp"=trim(stripslashes($value));
No other ideas in my head.
change $$key."_temp" to ${$key."_temp"} have a look on below solution:
$value = 'test';
$key="someuser";
${$key."_temp"}=$value;
echo $someuser_temp; //output test
Please try this ${$key . "_temp"} = trim(stripslashes($value));
This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 7 years ago.
I have the following code :
$_SESSION['aParties'] = time();
$aParties[] = $_SESSION['aParties'];
error_log(print_r($aParties,true), 3, "/var/tmp/error.log");
$first = reset(ksort($aParties));
The array aParties is like this :
Array
(
[0] => 1433841062
)
But I get the error :
'Only variables should be passed by reference in the method ksort'
Help me please! Thx in advance
You need to do it as
ksort($aParties);
$first = reset($aParties);
Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference.
Check Docs
This question already has answers here:
Enumerations on PHP
(39 answers)
Closed 2 years ago.
Does anyone know for php AbstractEnumeration if there is any way to do another level underneath it?
so like...
const a = 'a';
const b = 'b';
But I have an optional parameter for a:
const a = 'a' => '=123'
I know this is probably going to end up as a hash table instead, but just wondering what interesting things I can do with php enums.
PHP doesn't support native Enumerations.
You do something like:
abstract class ErrorCode
{
const NOT_FOUND = 404;
const OK = 200;
// etc.
}
$error = ErrorCode::NOT_FOUND;
This won't work in PHP:
const a = 'a' => '=123'
you could serialize the object as an array:
# serialize data into an array
define ("a", serialize (array ("a" => 123)));
# use it wherever you want
$a = unserialize (a);
This question already has answers here:
Is there a call_user_func() equivalent to create a new class instance?
(2 answers)
Closed 8 years ago.
I would like something like this but dynamically from an array like this:
$array = array("first","second","third");
So class would be called like this:
$class = new class("first","second","third");
You can use reflection for it:
$refClass = new ReflectionClass('SomeClass');
$instance = $refClass->newInstanceArgs($args);