Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Echo all same name variables in PHP

I have a simple php code for echo all variables in same name, I troubled to echo all variables. Here is my code

<?php 
$name="Baskaran";
$name="Udhayakumar";
$name="Venkatesh";
$name="Karthikeyan";

$names=array($name);

foreach($names as $value){
    echo $value . "<br>";
}

?>

In this code print only last name, Please guide me

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Each time you do $name= "name" you overwrite the previous entry in that variable.
You should start out with writing them into an array like so:

$names = []; // First create an empty array
$names[] ="Baskaran"; // Then add an entry to the first available key in that array
$names[] ="Udhayakumar"; // Repeat this
$names[] ="Venkatesh";
$names[] ="Karthikeyan";

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading