- Forums
- Snippets
- How To Combine Two Strings Into One
Today's Support Questions Is Regarding Php Programming We Received A Support Question Regarding How You Can Combine Or Put Together Different Strings To Execute As One [2861], Last Updated: Mon Jun 24, 2024
Webune Support
Fri Feb 05, 2010
2 Comments
611 Visits
today's support questions is regarding PHP programming.
Webune offers PHP hosting at low prices!!!
We received a support question regarding how you can combine or put together different strings to execute as one?
that was the question. as you can see its not very clear as to what the user wanted to achieve. after gathering more information, we became more aware that it really was a complicated question but the solution was simple.
we started to work with the user and ask him what he wanted to achieve or if we can get a code example of what he was trying to do. so this is what we got:
<?php
$PermsCmd1 = 'command 1';
$PermsCmd2 = 'command 2';
$PermsCmd3 = 'command 3';
$PermsCmd4 = 'command 4';
$PermsCmd5 = 'command 5';
for($counter =1; $counter <= 5; $counter++) {
echo ($PermsCmd.$counter).'<BR>';
}
?>
as you can see, we have a loop with 5 different strings, each string has begins with $PermsCmd and ends with a chronological number,
then we have a for loop. and we try to execute each string with this loop
what do you think the output is going to be?
this is what the user expected:
command 1
command 2
command 3
command 4
command 5
but that was not the case, instead the ouput looked like this:
1
2
3
4
5
to achieve the output the user wanted, we would have to use an array and our code would look like this:
<?php
$PermsCmd['1'] = 'command 1';
$PermsCmd['2'] = 'command 2';
$PermsCmd['3'] = 'command 3';
$PermsCmd['4'] = 'command 4';
$PermsCmd['5'] = 'command 5';
for($counter =1; $counter <= 5; $counter++) {
echo ($PermsCmd[$counter]).'<BR>';
}
?>
now the output will look like this:
command 1
command 2
command 3
command 4
command 5