Day 2 – Some basic programs in PHP

Now let’s write and run some basic programs in PHP.

Program 1: Program to print “Welcome to php” in PHP.

Open notepad++ and write below lines of code:

<?php
echo “Welcome to php”;
?>

Save this program in C://xampp/htdocs/welcome.php

Note: if xampp is not installed in C:// then save your program which ever drive xampp installed.

Don’t forget to start Apache from xampp controller panel whenever you want to run the PHP coding.

Open you browser and type following url : localhost/welcome.php

Output:

welcome

Explanation: PHP Code starts with “<?php” tag and ends with “?>” tag.
“echo” is use to display string or value on page.

Program 2: print function.

<?php
print “print is a function”;
?>

Output:
print is function

Explanation: “print” is a function to display string or value on page.

Question: Now the Question is what the difference between echo and print?

Answer:
echo can take and output multiple strings (See in Program 3).
print can’t output multiple string.
echo is marginally faster than print.
echo not return any value.
print return value either 0 or 1.

Program 3: echo with multiple strings.

<?php
echo “Hello Jhon “,”how are”,” you”;
?>

Output:
Hello Jhon how are you

Program 4: Print new line with the help of </br> tag.

<?php
echo “Going to start a new line”;
echo “</br> I am in new line.”;
?>

Explanation: <br> is a html tag, can use in PHP for next line.

Output:
Going to start a new line
I am in new line.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.