Skip to content

How to create your first PHP program

New Courses Coming Soon

Join the waiting lists

When learning a new programming language we have this tradition of creating a “Hello, World!” application. Something that prints those strings.

Make sure MAMP is running, and open the htdocs folder as explained above.

Open the index.php file in a code editor.

I recommend using VS Code, it’s a very simple code editor. See https://flaviocopes.com/vscode/ for an introduction.

This is the code that generates the “Welcome to MAMP” page you saw in the browser.

Delete everything and replace that with:

<?php
echo 'Hello World';
?>

Save, refresh the page on http://localhost:8888, you should see this:

Great!

That was your first PHP program.

Let’s explain what is happening here.

We have the Apache HTTP server listening on port 8888 on localhost, your computer.

When we access http://localhost:8888 with the browser we’re making an HTTP request, asking for the content of the route /, the base URL.

Apache by default is configured to serve that route serving the index.html file included in the htdocs folder. That file does not exist, but as we have configured Apache to work with PHP, it will then search for an index.php file.

That file exists, and PHP code is executed server-side before Apache sends the page back to the browser.

In the PHP file, we have a <?php opening, which says “here starts some PHP code”.

We have an ending ?> that closes the PHP code snippet, and inside it, we use the echo instruction to print the string enclosed into quotes into the HTML.

A semicolon is required at the end of every statement.

We have this opening/closing structure because we can embed PHP inside HTML. PHP is a scripting language, whose goal is to be able to “decorate” an HTML page with dynamic data.

Note that with modern PHP, we generally avoid mixing PHP into the HTML, and instead PHP is used as a “framework to generate the HTML”, for example using tools like Laravel. But we discuss plain PHP in this book, so it makes sense to start from the basics.

For example, something like this will give you the same result in the browser:

Hello
<?php
echo 'World';
?>

To the final user, that looks at the browser and has no idea of the code behind the scenes, there’s no difference at all.

The page is technically an HTML page, even though it does not contain HTML tags but just a Hello World string, but the browser can figure out how to display that in the window.

→ Get my PHP Handbook

Here is how can I help you:

  • COURSES where I teach everything I know
  • CODING BOOTCAMP cohort course - next edition in 2025
  • BOOKS 16 coding ebooks you can download for free on JS Python C PHP and lots more
  • Follow me on X

哆哆女性网起魏的名字360周转灵最容易下款的永城李振独掌轮回seo网络赢利的秘密周易八字起名名字大全完颜洛绒起名字的张姓猪宝宝起名字大全男孩陈失恋网名男北安制作网站女生中英混搭网名网站优化咨询电话定西湖南网站建设名字和周易卦象有关吗餐饮店墙绘湛江seo优化饮品取名起名大全少校seo技术好的算命的重庆二手网forte电子商务网站建设试题答案广告设计教程网站周易天行健君子以自强不息全文易家居算命网北京商业网站设计13骇人游戏 电影天堂周易起名图片比较炫酷的起名淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化