This question already has answers here:
PHP string number concatenation messed up
(5 answers)
Closed 8 years ago.
I wrote a simple script for echoing total of two variables to <h1> tag, but it gives strange results
<?php
$i=10; $j=20;
var_dump($i);
var_dump($j);
echo '<h1>'.$i+$j.'</h1>';
?>
Result of this script:
int(10)
int(20)
20</h1>
I was expecting 30 but it ate up <h1> tag, how can i do this as expected?
Try this:
echo '<h1>'.($i+$j).'</h1>';
This explains why you need to concatenate like this. Here is a working demo.
Related
This question already has answers here:
Json_encode or remove last comma?
(3 answers)
How do I remove a comma off the end of a string? [duplicate]
(10 answers)
Closed 1 year ago.
I want to remove the last comma from below array.
{"location":"Park Town,Chennai,Tamil Nadu" },
{"location":"Arajar Salai,Simmakkal,Madurai" },
Jon Stirling have right in his comment. If you try to create json manually - it's bad idea.
But anwser to your question should be something like this (didn't test it) :
<?php
$tab=explode(",",$string);
array_pop($tab);
$string=implode(",",$tab).']';
?>
Just use php rtrim() function on the string you will get your result.
like below
<?php
$text = '{"location":"Park Town,Chennai,Tamil Nadu" },{"location":"Arajar Salai,Simmakkal,Madurai" },';
$test = rtrim($text,',');
echo $test;
?>
A live example https://eval.in/849038
This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
i have a problem.
I want in php to replace all 'blablabla' with 'bla'(because i hate blablabla). Here's my code:
<?php
$string = 'Dracula always says BlaBlaBla but says he never says BlaBlaBla';
$result = answer to replace here
?>
Thanks
Use srt_replace function to do that like this :
str_replace("BlaBlaBla","bla",$string);
You can use something like this:
str_replace("BlaBlaBla","bla",$string)
Also you can find it here in the docs: http://php.net/manual/en/function.str-replace.php
This question already has answers here:
How to enable PHP short tags?
(21 answers)
Closed 6 years ago.
Essentially I am trying to have a heading using what is below. Not sure what is going on but nothing happens. Even when I do something as simple as echo "hello" there instead of the 2 variables.
<h1><?echo "$info[0] ($info[1])";?></h1>
You can use <?= ?> shorthand for echoing variables and the code should look like,
<h1><?= "$info[0] ($info[1])"; ?></h1>
Try using this way. You are missing <?php.
<h1> <?php echo $info[0] .'('. $info[1] .')'; ?> </h1>
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 6 years ago.
Ya here is my code.
<?php print "<?php echo $this->lang('learn_cn')?>" ?>
It comes with blank nothing show.
<?php echo $this->lang('learn_cn')?>
What is the correct way to make this work. TY
You're already in the process of printing out HTML, so just escape that part like you did in the href:
<?php print "" . $this->lang('learn_cn') . "" ?>
This question already has answers here:
Reference: Comparing PHP's print and echo
(2 answers)
Closed 7 years ago.
Which statement should I be using in PHP? Print or Echo?
In PHP, Echo and Print do the same thing.
Whats the difference? is one of them faster?
Thanks!!
This is a duplicate. Please refer to:
How are echo and print different in PHP?