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>";
}
Related
This question already has answers here:
How to append data to file using file_put_contents()?
(2 answers)
Closed 2 years ago.
I have a script that moves through a list and if a condition is met, it echoes the line.
Now, rather than echo the line, I'd like for that line to be placed in a txt file.
Here's my code at the moment:
<?php
$list = file('list.txt');
foreach ($list as $x)
{
if (condition1istrue)
{
echo "$x - Condition 1";
}
else if (condition2istrue)
{
echo "$x - Condition 2";
}
else
{
echo "Conditions Not Met";
}
}
?>
Problem: When I use the below code in place of the echo statement, it only adds the last line where the condition is true and deletes all other lines where the conditions were also true.
$file = fopen('office.txt', 'r');
file_put_contents('true.txt', $x);
So build up the file content incrementally in a variable, then write it at the end.
$content = '';
foreach($foo as $bar) $content .= "Another line\r\n";
file_put_contents('file.txt', $content);
This question already has answers here:
How can I loop through a MySQL result set more than once using the mysql_* functions?
(7 answers)
PDO multiple fetching of same query [duplicate]
(2 answers)
Closed 3 years ago.
I have a array and i use two foreach but I be only able to print the first foreach.
$test = $db->query("SELECT * FROM Test");
foreach ($test as $readTest) {
echo $readTest["col1"];
}
foreach ($test as $readTest) {
echo $readTest["col2"];
}
and I try with this:
$test1 = $test;
$test2 = $test;
I expect the output of $readTest["col1"] and $readTest["col2"], but the actual output is $readTest["col1"]
Use #fetchAll to get all your result in a conventional array. You will then be able to loop on them multiple times.
Why use array, when you can print this way:
$test = $db->query("SELECT * FROM Test");
while($PrintTest = $test->fetch_object())
{
echo $PrintTest->col1;
echo $PrintTest->col2;
}
Even if you want to use Arrays, you can do something like this:
$Array1 = array();
$Array2 = array();
$test = $db->query("SELECT * FROM Test");
while($PrintTest = $test->fetch_object())
{
$Array1[] = $PrintTest->col1;
$Array2[] = $PrintTest->col2;
}
And then, you can run through the array with For or Foreach
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:
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'
This question already has answers here:
Imploding an associative array in PHP
(10 answers)
Closed 8 years ago.
I have a named array that look like this:
$arr = array('name'=>'somename','value'=>'somevalue');
I want to turn that array into something like this:
name='somename' value='somevalue'
I tried http_build_query() to do it.
echo http_build_query($arr, '', ' ');
But this is the result I get:
name=somename%21 value=somevalue%21
How can I get the result I want with http_build_query()? Or, is there any PHP function to do it?
Thank you.
http_build_query returns a urlencoded string. If you don't want that you can run it through urldecode
$arr = array('name'=>"'somename'",'value'=>"'somevalue'");
print urldecode(http_build_query($arr,null,' '));
Try with foreach()
$arr = array('name'=>'somename','value'=>'somevalue');
$str = '';
foreach($arr as $k=>$v) {
$str.= "$k='$v' ";
}
echo $str;
foreach ($array as $key => $value) {
$result .= "$key='$value' ";
}
echo rtrim($result,' ');