Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

PHP Cookbook: Interpolating Functions and Expressions Within Strings

Posted on March 7, 2012

Problem
You want to include the results of executing a function or expression within a string.

Solution
Use the string concatenation operator (.), as shown in Example 1-27, when the value you want to include can’t be inside the string.

Example 1-27. String concatenation
[sourcecode language=”php”]
<?php
print ‘You have ‘.($_REQUEST[‘boys’] + $_REQUEST[‘girls’]).’ children.’;
print "The word ‘$word’ is ".strlen($word).’ characters long.’;
print ‘You owe ‘.$amounts[‘payment’].’ immediately’;
print "My circle’s diameter is ".$circle->getDiameter().’ inches.’;
?>
[/sourcecode]

Discussion
You can put variables, object properties, and array elements (if the subscript is unquoted) directly in double-quoted strings:

[sourcecode language=”php”]
<?php
print "I have $children children.";
print "You owe $amounts[payment] immediately.";
print "My circle’s diameter is $circle->diameter inches.";
?>
[/sourcecode]

Interpolation with double-quoted strings places some limitations on the syntax of what can be interpolated. In the previous example, $amounts[‘payment’] had to be written as $amounts[payment] so it would be interpolated properly. Use curly braces around more complicated expressions to interpolate them into a string. For example:
[sourcecode language=”php”]
<?php
print "I have less than {$children} children.";
print "You owe {$amounts[‘payment’]} immediately.";
print "My circle’s diameter is {$circle->getDiameter()} inches.";
?>
[/sourcecode]
Direct interpolation or using string concatenation also works with heredocs. Interpolating with string concatenation in heredocs can look a little strange because the closing heredoc delimiter and the string concatenation operator have to be on separate lines:
[sourcecode language=”php”]
<?php
print <<< END
Right now, the time is
END
. strftime(‘%c’) . <<< END
but tomorrow it will be
END
. strftime(‘%c’,time() + 86400);
?>
[/sourcecode]
Also, if you’re interpolating with heredocs, make sure to include appropriate spacing for the whole string to appear properly. In the previous example, Right now the time has to include a trailing space, and but tomorrow it will be has to include leading and trailing spaces.

See Also
For the syntax to interpolate variable variables (such as ${“amount_$i”}), see Recipe 5.4; documentation on the string concatenation operator at http://www.php.net/language.operators.string.

Terbaru

  • Inilah Cara Pakai Google Maps Offline Biar Mudik Lebaran 2026 Nggak Nyasar Meski Tanpa Sinyal!
  • Inilah Alasan Mahkamah Agung Tolak Kasasi Google, Denda Rp202,5 Miliar Resmi Menanti Akibat Praktik Monopoli
  • Inilah Cara Daftar dan Syarat SPMB SMK Boarding Jawa Tengah 2026, Sekolah Gratis Sampai Lulus!
  • Inilah Daftar Sekolah Kedinasan 2026 untuk Lulusan SMK, Bisa Kuliah Gratis dan Berpeluang Besar Langsung Jadi CPNS!
  • Inilah Pajak TER: Skema Baru PPh 21 yang Nggak Bikin Pusing, Begini Cara Hitungnya!
  • Inilah Jadwal Resmi Jam Buka Tol Jogja-Solo Segmen Prambanan-Purwomartani Saat Mudik Lebaran 2026
  • Inilah Cara Mendapatkan Witherbloom di Fisch Roblox, Rahasia Menangkap Ikan Paling Sulit di Toxic Grove!
  • Kenapa Indomart Point Bisa Kalahkan Bisnis Kafe?
  • Inilah Cara Mendapatkan Rotten Seed di Fisch Roblox, Lokasi Rahasia di Toxic Grove Buat Unlock Toxic Lotus!
  • Inilah Cara Zakat Crypto Kalian Bisa Jadi Pengurang Pajak Berdasarkan Aturan Resmi Pemerintah!
  • Inilah Perbandingan Airwallex vs Payoneer 2026: Jangan Sampai Profit Kalian Ludes Gara-Gara Biaya Admin!
  • Inilah Roadmap 7 Tahap Bangun Bisnis Digital dari Nol Biar Nggak Cuma Putar-Putar di Tempat!
  • Inilah Cara Tetap Gajian dari YouTube Meski View Masih Ratusan, Penasaran?
  • Inilah Alasan Akun TikTok Affiliate GMV 270 Juta Kena Banned Permanen!
  • Inilah Bahaya Astute Beta Server APK, Jangan Sembarang Klik Link Download FF Kipas 2026!
  • Inilah Bahaya Nonton Film di LK21 dan IndoXXI, Awas Data Pribadi dan Saldo Rekening Kalian Bisa Ludes!
  • Inilah Kronologi & Video Lengkap Kasus Sejoli Tambelangan Sampang Viral, Ternyata Gini Awal Mulanya!
  • Inilah Alasan Kenapa Koin Nego Neko Shopee Nggak Bisa Dipakai Bayar Full dan Cara Rahasia Dapetinnya!
  • Inilah Cara Menjawab Pertanyaan Apakah di Sekolahmu Sudah Ada IFP/PID dengan Benar dan Profesional
  • Inilah Fakta Isu Roblox Diblokir di Indonesia 2026, Benarkah Akan Ditutup Total?
  • Inilah Penyebab dan Cara Mengatasi FF Kipas My ID Verify UID Biar Akun Tetap Aman
  • Inilah Deretan HP RAM 8GB Harga di Bawah 2 Juta Terbaik 2026, Spek Dewa Tapi Nggak Bikin Kantong Jebol!
  • Contoh Cara Buat SK Panitia TKA 2026
  • Inilah Cara Download Point Blank ID Versi Terbaru 2026, Gampang Banget Ternyata!
  • Inilah Persiapan Lengkap Gladi Bersih TKA 2026 SD dan SMP: Jadwal, Teknis Proktor, dan Aturan yang Wajib Kalian Tahu
  • Inilah Aturan Lengkapnya, Apakah Zakat dalam Bentuk Barang Bisa Mengurangi Pajak Penghasilan Kalian?
  • Inilah Kenapa KBLI Sangat Penting Buat Bisnis Digital dan Gini Caranya Biar Kalian Nggak Salah Pilih Kode
  • Inilah Fitur Keren ONLYOFFICE Docs 9.3, Cara Baru Edit PDF dan Dokumen Lebih Efisien!
  • Inilah Cara Banjir Komisi Shopee Affiliate Hanya Dalam 7 Hari Saja
  • Cara Download Aplikasi BUSSID Versi 3.7.1 Masih Dicari dan Link Download Aman Mediafire
  • How to Master Cloud Infrastructure with Ansible and Terraform
  • How to Fix VirtualBox Stuck on Saving State: A Complete Guide
  • How to Run Windows Apps on Linux: A Complete Guide to WinBoat, WINE, and Beyond
  • Build Your Own AI Development Team: Deploying OpenClaw and Claude Code on a VPS!
  • How to Measure Real Success in the Age of AI: A Guide to Software Metrics That Actually Matter
  • How to LLM Finetuning with FPT AI Factory
  • New ComfyUI Released, The App Mode is Amazing
  • How to Master Kilo Code for VS Code: Use Parallel AI Agents to Build Software Faster than Ever Before
  • 4 Massive Claude Code 2.0 Updates You Must Master
  • How to Build Your Own Website from Scratch: A Beginner’s Guide to Mastering HTML and CSS
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025

©2026 emka.web.id | Design: Newspaperly WordPress Theme