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}
?>
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:
What are the PHP operators "?" and ":" called and what do they do?
(10 answers)
How to check if $data variable is set with Codeigniter?
(6 answers)
Closed 4 years ago.
Hey I'm new to php and codeigniter. I know codeigniter has an isset function.
what does the following code mean? Can someone please help
<?php echo isset($error) ? $error : ''; ?>
isset is a php function, you can use it without CodeIgnitor, but it basically checks to see if the variable has been set yet.
$someVariable = 'This variable has been set';
var_dump(isset($someVariable)); // True
var_dump(isset($anotherVariable)); // False
the ? and : parts tell PHP what to do. It's called a ternary operator, and can be thought as as a short if statement:
echo isset($someVariable) ? 'set' : 'not set';
is the same as:
if (isset($someVariable)) {
echo 'set';
} else {
echo 'not set';
}
http://php.net/manual/en/function.isset.php
http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
This question already has answers here:
Passing an optional parameter in PHP Function [duplicate]
(6 answers)
Closed 7 years ago.
Okay - I was able to the pass a string assigned to the variable $myfile into the function. If it could be better, please provide feedback- learning PHP.
<?php
$myfile = $row_rs_recordview['headstone'];
echo "$myfile"; // verify the file name
function readGPSinfoEXIF($myfile)
{
global $myfile;
$exif= exif_read_data("headstone/".$myfile, 0, true); //
if(!$exif || $exif['GPS']['GPSLatitude'] == '') //Determines if the
//geolocation data exists in the EXIF data
{
return false; //no GPS Data found
echo "No GPS DATA in EXIF METADATA";
}
?>
Insights appreciated!
Thanks
you can't access the global variables inside function directly.
//add global before variable declaration like this
function fun(){
global $row_rsUpdate['headstone'];
}
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php variable = variable1 || variable2
Trying to do this in PHP evaluates to true, instead of returning 'nothing' like js does.
//Javascript
var stuff = false;
document.write(stuff || 'nothing');
So I have to do this. Is there anyway to avoid typing the variable stuff twice?
//PHP
$stuff = false;
echo !empty($stuff)?$stuff:'nothing';
If you use PHP 5.3 or later you can use the shorthand ternary format:
echo ($stuff) ?: 'nothing';