PHP return number format [duplicate] - php

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?

Related

How to get random value from enum class in PHP? [duplicate]

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>

How to use the variable inside some function? [duplicate]

This question already has answers here:
How to pass a variable inside a function?
(1 answer)
How to access array element inside another array [duplicate]
(1 answer)
Closed 5 years ago.
I have an API's Function :
$transaction=$tran[4];
function coinpayments_api_call($cmd, $req = array(),$transaction) {
curl_init($transaction);
}
$transaction variable not passing into function coinpayments_api_call.
function not taking values from out side.
I also make $transaction varible GLOBAL ,but still same problem ,
Please Help
Make your code like this
$transaction=$tran[4];
function coinpayments_api_call($transaction,$cmd, $req = array(),$txnid) {
curl_init($transaction);
}

PHP - auto assign a different name to a variable of variable [duplicate]

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));

Store values in an array and pass it in url [duplicate]

This question already has answers here:
Passing arrays as url parameter
(11 answers)
Closed 9 years ago.
$deposit=$_POST['amountdeposit'];
$arr= array();
for($i=0;$i<10;$i++)
{
if($arr[$i]=='\0')
{ $arr[$i]= array("$deposit");
}
break;
}
$page= "step2.php?arr=$arr";
header("Location:$page");
?>
what i want to do is each time there's a change in $deposit , this value is stored in $arr[$i] and then it is passed in a url so that i could use GET on that step2.php page.
What I see is just arr=array instead of values :/ please help me.
You want http_query_string. It will do exactly what you want.
A couple of other comments have recommended http_query_string, however I would use serialize along with urlencode.
Replace:
$page= "step2.php?arr=$arr";
with:
$page= "step2.php?arr=" . urlencode(serialize($arr));
Then when you get to step2.php, unserialize(urldecode($_GET['arr'])) will contain your array as you originally built it.

Get username from session [duplicate]

This question already has answers here:
How Can I get a Session Id or username in php?
(3 answers)
Closed 9 years ago.
I'm trying to get the member username from the session and can't find a real way
This is the code I reached:
<?php
session_start();
if( $session_name['member_username']="admin")
{//do something
} else { do smth}
Thank you
Use double equal to (==) for comparison inside if
<?php
session_start();
if( $_SESSION['member_username']=="admin")
{//do something
} else { do smth}
?>

Categories