Variable into another variable - php

I have a variable ($uid) which fetches value from a database. My problem is that I am not able to append that variable to another variable ($str). The value of the $uid will be in place of '___' in the $str variable. How do I do approach?
$uid = 5;
$str ='ALPHA|___|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA';

//use this code
$uid = 5; $str ='ALPHA|___|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA';
echo str_replace("___",$uid,$str);

try this
$uid = 5;
$str ='ALPHA|___|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA';
echo str_replace("___",$uid,$str);
output : ALPHA|5|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA

Related

How to add string to a PHP variable?

I'm not sure it's even the right way to define this question.
I have string that may be exist, and may not. It happens to be a number: $number
If $number doesn't exist, then I want to use the PHP variable $url.
But if $number does exist, then I want to use the PHP variable which is named $url+the number, i.e, $url2 if $number=2
So I tried this code, but it doesn't work:
$number = "2"; //(Can be either missing, or equal to 1, 2, or 3)
$url = "www.0.com"; // Fallback
$url1 = "www.1.com";
$url2 = "www.2.com";
$url3 = "www.3.com";
$result = $url.=$number ;
// If $number=1, I want $result to be : www.1.com
// If $number=2, I want $result to be : www.2.com
// If $number=3, I want $result to be : www.3.com
// If $number IS NOT SET, I want $result to be : www.0.com
// Now do something with $result
Perhaps there's a completely better way to achieve what I want (will be happy to see example), but anyway I'm curious as well to understand how to achieve it my way.
Okay, so you're talking about a variable variable.
You should define the name of the variable you need to use in a string, and then pass that to a variable variable using $$ syntax:
if( isset($number) && is_numeric($number) )
{
$name = 'url'.$number;
$result = $$name;
}
else
{
$result = $url;
}
That having been said, you may be better off using an array for this:
$urls = [ 'www.0.com', 'www.1.com', 'www.2.com', 'www.3.com' ];
$result = (!isset($number)) ? $urls[0] : $urls[ intval($number) ];
You can use ternary with in_array and empty.
$number = "2"; //(Can be either missing, or equal to 1, 2, or 3)
$url = "www.0.com"; // Fallback
$url1 = "www.1.com";
$url2 = "www.2.com";
$url3 = "www.3.com";
$result = (!empty($number) && in_array($number, array(1,2,3))) ? ${'url' . $number} : $url;
echo $result;
Demo: https://eval.in/821737
In php you can have things like dynamic variable names:
$variableName = "url".$number;
$result = $$variableName;
However, you should make sure, that $variableName refers to an existing variable:
$result = "www.fallbackURL.com";
if(isset($$variableName)) $result = $$variableName;
Or Try this code:
$number = 5;
$url[0] = "www.0.com"; // Fallback
$url[1] = "www.1.com";
$url[2] = "www.2.com";
$url[3] = "www.3.com";
if (!isset($number)) $number=0;
if (!isset($url[$number])) $number=0;
$result = $url[$number];
If you add $ front of string, it define variable, so you can use following code:
<?php
$number = "2"; //(Can be either missing, or equal to 1/2/3)
$url = "www.0.com"; // Fallback
$url1 = "www.1.com";
$url2 = "www.2.com";
$url3 = "www.3.com";
if(isset($number) && is_numeric($number) && $number <= 3) {
$variable_name = 'url' . $number; //string like url2
} else {
$variable_name = 'url';
}
$result = $$variable_name ; //define $url2 from url2 string
echo $result;
// Now do something with $result
Example for define variable with string variable:
$string = 'hello';
$$string = 'new variable'; //define $hello variable
echo $hello; //Output: "new variable"
if the url need just a number, you can do this easy way
($number)?$number:0;
$url = "www.".$number.".com";
if there are specific real url, you can use array
$array[0] = "www.google.com";
$array[1] = "www.facebook.com";
($number)?$number:0;
url = $array[$number];
Updated code:
$number = "2";
if(isset($number)){
$res = "url".$number;
$result=$$res;
}else{
$result=$url;
}
echo $result;

How do i get a variable by concatenating?

I understand there are topics which discuss concatenation.
However, i don't seem to figure out what's wrong with this
$LEVEL0 = "Banned";
$LEVEL1 = "Member";
$LEVEL2 = "Subscriber";
$Level = "LEVEL".$userRow['user_level'];
echo($Level);
It would always just echo the number stored in $userRow['user_level']
rather than echo the value of the variable itself.
Use Variable variables.
$LEVEL0 = "Banned";
$LEVEL1 = "Member";
$LEVEL2 = "Subscriber";
$Level = "LEVEL" . $userRow['user_level'];
echo $$Level;
I think you are looking for this:
$LEVEL = array("Banned","Member","Subscriber");
$Levelval = $LEVEL[$userRow['user_level']];
echo $Levelval;

when to pass a variable as an address in a function in php

I know what ampersand means in a function, it means you have to pass the variable by address. But i have not encountered a scenario wherein i need to pass some variable as an address. Can you guys clarify this for me? like for example
function dollarx($x) {
echo $x[1];
}
function dollar(&$x) {
echo $x[1];
}
$x[0] = 1;
$x[1] = 2;
two functions displays the same
You pass "by reference" when you want the function to be able to update the value of the variable.
For example:
function dollar(&$x){
$x[1] = 12;
}
$x = array(1, 2);
dollar($x);
var_dump($x); // array(1, 12);
You could use it to have a function set variables for you. For example you could have a function return an array with results, or you could use variables by reference.
function GetUsernameAndEmailAddress(&$user, &$email) {
//get values from database or session or whatever.
$user = 'username';
$email = 'email#example.com';
}
$username = '';
$email = '';
GetUsernameAndEmailAddress($username, $email);
echo 'Hello '. $username. '. I have sent an e-mail to '.$email;

Dynamically setting a variable in PHP?

So i want to deo something like this and not sure how
for($s=0; $s < 5; $s++ ){
$pre_config_query = "select * from preconfig where code = '{$industry_string}_{$s}_{$class_string}'";
$pre_config_station = mysql_query($pre_config_query);
$it_exists = mysql_num_rows($pre_config_station);
if($it_exists>0){
$pre_config = mysql_fetch_assoc($pre_config_station);
$pre{$s} = $pre_config['id'];
I want the end product to have these 5 variables named
print $pre1;
print $pre2;
print $pre3;
print $pre4;
print $pre5;
That have the $pre_config['id'] if present....any ideas
You can use variable variables to accomplish that.
First, define a variable with the desired name:
$varname = "pre$s";
Second, assign a value to it:
$$varname = $pre_config['id'];
That's all!
this works but I'm not sure I'm answering your question.
<?php
for($s=1; $s < 6; $s++ ){
$it_exists=1;
if($it_exists > 0){
$pre_config = array('id'=>rand(10,99));
${"pre".$s} = $pre_config['id'];
}
}
echo $pre1."<br/>";
echo $pre2."<br/>";
echo $pre3."<br/>";
echo $pre4."<br/>";
echo $pre5."<br/>";
?>

PHP - Variable inside variable?

$bookA = "123";
$crack = "A";
I want to do something similar to this:
echo $book$crack;
Such that the output is 123.
What is the correct syntax for the echo command?
Thanks.
echo ${"book" . $crack};
These are called variable variables, but you should use arrays instead.
$varname = 'book'.$crack;
echo $$varname;
You might want to use an associative array.
For instance:
$book = array();
$book["A"] = "Some Book";
$crack = "A";
//Later
echo $book[$crack];
This will work:
$bookA = "123";
$crack = "A";
$var = "book$crack";
echo $$var;
Try the following:
echo ${book.$crack};
It works for me.

Categories