Add value to a variable from another variable? [duplicate] - php

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 1 year ago.
these are two variables with two different values.
$variableone = 'This';
$variabletwo = 'Place';
How do I add variable to to variable one like this with a empty space between "This" and "Place":
$variableone = 'This Place';
and how do i update variable two to have no value like this
$variabletwo = " ";
I would apperciate a answer thanks

You can use string concatenation
$variableone = 'This';
$variabletwo = 'Place';
$variableone .= (" " . $variabletwo);
$variabletwo = " ";
or string substitution
$variableone = 'This';
$variabletwo = 'Place';
$variableone = "$variableone $variabletwo";
$variabletwo = " ";

Related

Can I set variable value by name in another variable in PowerShell [duplicate]

This question already has an answer here:
Dynamically create variables in PowerShell
(1 answer)
Closed 2 years ago.
In PHP I can write that code:
$var = 1;
$name = 'var';
$$name = 2;
echo "var = $var";
Result: var = 2
How can I do things like that in PowerShell?
I tried:
$$name = 2
Unexpected token 'name' in expression or statement.
${$name} = 2
Creates variable with name $name
You can accomplish this by assigning a variable by reference.
$var = 1
$name = [ref]$var
$name.Value = 2
$var # outputs 2

Can not compare two date using PHP [duplicate]

This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 7 years ago.
I have an issue while comparing two date inside if condition. I am providing my code below.
$erresult = mysqli_fetch_array($qrylast);
$ticket = $erresult['ticket_id'];
if ((date("Y-m-d") == $erresult['date'])) {
$id = sprintf("%03d", $ticket++);
$fields = array("date", "ticket_id ");
$tablename = "db_ticket";
$values = array(date("Y-m-d"), $id);
$id1 = db_insert($tablename, $values, $fields);
if ($id1) {
$ticket_id = 'W1' . date("Ymd") . $id;
}
} else {
$id = '001';
$fields = array("date", "ticket_id ");
$tablename = "db_ticket";
$values = array(date("Y-m-d"), $id);
$id1 = db_insert($tablename, $values, $fields);
if ($id1) {
$ticket_id = 'W1' . date("Ymd") . $id;
}
}
Here I need to compare today's date with date save inside database. My saved date inside database datatype is also date but here always else part is executing. In my code I have one condition (date("Y-m-d")==$erresult['date']) and this condition is never executing even two date are same.
Try
if(strtotime(date("Y-m-d")) == strtotime($erresult['date']))
follow :- How to compare two dates in php
You can use the strtotime() function to compare the two dates
e.g
if(strtotime(date("Y-m-d")) == strtotime(date("Y-m-d",$erresult['date'])))

Correct way of combining variables? [duplicate]

This question already has answers here:
How to join strings in PHP?
(3 answers)
Closed 7 years ago.
I have the following -
function button_class_add() {
global $uno_dos;
$btn_shape_class = $uno_dos['buttons-shape-select-general'];
$btn_size_class = $uno_dos['buttons-size-select-general'];
?>
<script type="text/javascript">
jQuery(function($) {
$('.std-button').addClass('<?php echo $btn_shape_class $btn_size_class; ?>');
});
</script>
<?php }
add_action('wp_footer', 'button_class_add');
How can I correctly combine these 2 variables so I can just echo out the combined variable?
$btn_shape_class = $uno_dos['buttons-shape-select-general'];
$btn_size_class = $uno_dos['buttons-size-select-general'];
Many thanks
Combining these lines -
$btn_shape_class = $uno_dos['buttons-shape-select-general'];
$btn_size_class = $uno_dos['buttons-size-select-general'];
Into something like -
$btn_shape = $uno_dos['buttons-shape-select-general']$uno_dos['buttons-size-select-general'];
Then I can just echo the 1 variable.
You can just use the "." string operator:
http://php.net/manual/en/language.operators.string.php
$combined = $btn_shape_class.$btn_size_class
or if you want to have a whitespace between, like in your current code:
$combined = $btn_shape_class.' '.$btn_size_class
It looks like by "combine", you mean "concatenate". Concatenating allows you to append a string to the end of another string.
If you want to concatenate two variables with different values then you just use a fullstop as follows:
// $variable_1 = 'hot';
// $variable_2 = 'dog';
echo $variable_1 . $variable_2;
// 'hotdog'
If you want to add whitespace between the two variables, on the other hand, you would need to concatenate the whitespace as follows:
// $variable_1 = 'hot';
// $variable_2 = 'dog';
echo $variable_1 . ' ' . $variable_2;
// 'hot dog'

Variable within a variable [duplicate]

This question already has answers here:
Print string with a php variable in it
(4 answers)
Simple PHP stuff : variable evaluation [duplicate]
(5 answers)
Closed 11 months ago.
I can't seem to wrap my head around this for some reason.
$welcome_message = "Hello there $name";
$names_array = array("tom", "dick", "harry");
foreach ($names_array as $this_name) {
$name = $this_name;
echo $welcome_message."<br>";
}
How do I update the $name variable within $welcome_message each time?
Using variable variables but I can't seem to make it work.
Thanks
Maybe you're looking for sprintf?
$welcome_message = "Hello there %s";
$names_array = array("tom", "dick", "harry");
foreach ($names_array as $this_name) {
echo sprintf($welcome_message, $this_name), "<br>";
}
This won't work because $welcome_message is evaluated just once, at the beginning (when $name is probably still undefined). You cannot "save" the desired form inside $welcome_message and "expand" it later at will (unless you use eval, and that's something to be totally avoided).
Move the line that sets $welcome_message inside the loop instead:
$names_array = array("tom", "dick", "harry");
foreach ($names_array as $this_name) {
$welcome_message = "Hello there $this_name";
echo $welcome_message."<br>";
}
you can update the $welcome_message each time like this....
$welcome_message = "Hello there ".$name;
now the code will be like this...
$welcome_message = "Hello there ";
$names_array = array("tom", "dick", "harry");
foreach ($names_array as $this_name) {
echo $welcome_message.$this_name"<br>";
}

For two dimentional array ERROR=Fatal error: Cannot use string offset as an array in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Cannot use string offset as an array” error
<?php
$marks = 0;
$total = $_POST["lines"];
$quesA = $_POST["ques"];
$quesNo = 1;
$opt = array("","A","B","C","D");
for ($i = 0; $i < $total; $i++)
{
$q = $quesA[$i][0]; // here error
...
Here I assign $quesA = $_POST["ques"]; now it take a variable $q = $quesA[$i][0]; then print in line
echo "<div><b style='color:red;'>Q" . $quesNo . " : </b>"
. "<span style='color:blue;'>" . $q ."</span></div>"
;
here is my code: https://dl.dropbox.com/u/24972447/onlineQuiz.7z
The problem is that $quesA[$i] is not a valid array and you are trying to use it like an array.
Am not sure how you are getting $_POST["ques"] but you need to make sure its an array before you can access it.
Post what you are expecting from $_POST["lines"] & $_POST["ques"]; i may be able to help further
After
$quesA=$_POST["ques"];
$quesA is a string, not a multidimensional array, so $quesA[$i] gives you the (i+1)th character of the string (the "string offset" of the error message).
You cannot use the index operator on that.

Categories