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'
Related
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 = " ";
This question already has answers here:
explode string into variables
(2 answers)
Closed 5 years ago.
I need to use php and extract the items from the string and assign them to variables.
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
How can I get:
$var1 = 76305203
$var2 = 124400884
To create variables use list()
<?php
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
list($var1,$var2) = explode(';', $string);
echo $var1;
echo PHP_EOL;
echo $var2;
Output:- https://eval.in/928536
Or use explode() to get array and use that array
<?php
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
$array = explode(';', $string);
echo $array[0];
echo PHP_EOL;
echo $array[1];
Output:-https://eval.in/928537
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 5 years ago.
Apologies in advance though I've tried and failed several different things and obviously I'm not a php pro just yet.
I'm looking for a way to tidy up my code here, I'm pretty certain I don't need to continually type "echo" for each line but can't work out how to combine my code to achieve the same result, any ideas?
<?php
$values = get_field('bothscoreno');
$url = "$values";
$arr = str_split("$url, PHP_URL_QUERY");
if ($url) {
echo $arr[11];
echo $arr[12];
echo $arr[13];
echo $arr[14];
echo $arr[15];
echo $arr[16];
} else {
echo 'No';
}
?>
Thanks in advance.
The code you posted probably has bugs, because the way it's written, it looks like it's intended to do something different from what it is actually going to do.
str_split() will take a string and output an array of single characters. It looks like you're trying to pass it two parameters, but enclosing them in quotes means it's actually just a single string.
Thus, if $url is equal to abc, your str_split() call will output an array of a, b, c, ,, , P, H, P, _, U, R, L, _ ... etc.
I don't think that's what you intended.
However, if it is what you intended, then you are splitting the string, only to re-join some of the characters back together again with echo. You can therefore can simplify the whole thing as follows:
$url = get_field('bothscoreno');
if ($url) {
echo substr($url, 11, 6);
} else {
echo "No.";
}
If I'm right and this isn't what you actually want to do, then I suggest either editing the question to clarify or asking a whole new one.
use for loop and start index from 14 to echo your result
$values = get_field('bothscoreno');
$url = $values;
$arr = str_split("$url, PHP_URL_QUERY");
$string = '';
if ($url) {
for($i = 11;$i <= 16;$i++){
$string .= $arr[$i];
}
} else {
$string = 'No';
}
echo $string;
Use the point as concatenation operator
echo $arr[11].$arr[12]
in PHP you're able to use a . as concatenation operator.
See the code below:
<?php
$values = get_field('bothscoreno');
$url = "$values";
$arr = str_split("$url, PHP_URL_QUERY");
if ($url) {
echo $arr[11].
$arr[12].
$arr[13].
$arr[14].
$arr[15].
$arr[16];
} else {
echo 'No';
}
Hope this helps!
Use array_slice
<?php
$values = get_field('bothscoreno');
$url = "$values";
$arr = str_split("$url, PHP_URL_QUERY");
if ($url) {
$subArr = array_slice($arr, 11, 6);
print_r($subArr);
// Or...
foreach ($subArr as $val) {
echo $val;
}
} else {
echo 'No';
}
?>
This question already has answers here:
concat a for loop - php
(3 answers)
Closed 2 years ago.
Is it possible to combine two strings with for loop?
For example:
echo 'Prefix '.for($i=0;$i<4;$i++){ echo $i; }.' suffix';
This is not possible:
echo 'Prefix ';
for($i=0;$i<4;$i++)
{
echo $i;
}
echo ' suffix';
Because I would like to save a page using file_put_contents and source has a combination of HTML and PHP.
I would like to get:
$page = <beginning_of_html_page_here>
<php_code_here>
<end_html_page_here>
file_put_contents(page.html, $page);
You can use string concatenation. Use the dot . to join to strings, 'a'.'b' will give 'ab'. And $a .= 'c' will append 'c' to the $a variable.
// Create the string
$string = 'Prefix ';
for($i=0;$i<4;$i++)
{
// Append the numbers to the string
$string .= $i;
}
// Append the suffix to the string
$string .= ' suffix';
// Display the string
echo $string;
Result is:
Prefix 0123 suffix
Demo at Codepad.
About the end of your question, you can use this logic:
$page = '<beginning_of_html_page_here>';
// Append things to your string with PHP
$page .= 'something'
$page .= '<end_html_page_here>';
About your first code block, this can also be done by using two functions: range() to generate an array of numbers and implode() to join the array's items:
<?php
// Create the string
$string = 'Prefix '.implode('', range(0, 3)).' suffix';
echo $string;
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>";
}