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

  • Kronologi Jatuhnya Pesawat ATR 400 Yogyakarta-Makassar di Maros
  • Bosen Nyupir Bus Terus? Ini 9 Mod Bussid Motor Honda Paling Keren Buat Kalian Coba!
  • Saldo DANA Gratis dari Kode Redeem Terbaru 2026, APK JadiDuit Penipu?
  • Ngefans Maxwell Clash of Champions? Begini Cara Masuk Saluran WA-nya Biar Update Terus!
  • Ini Cara Pakai greatonlinetools.com buat Nambah Likes dan Iseng ke Teman!
  • Cara Pakai Bug Token M7 Mobile Legends, Banjir Skin Prime Cuma Modal Browser!
  • Pusing Gagal Simpan Data Internet Dapodik 2026.b? Ini Trik Jitu Mengatasinya!
  • Mau Cuan Tambahan? Ini Cara Mudah Jadi Clipper di Ternak Klip Modal HP Doang!
  • Akun PTK Silang Merah di Dapodik 2026.b Bikin Panik? Jangan Dihapus, Coba Trik Ini Dulu!
  • Ini Trik Supaya Bisa Mancing Otomatis di Fish It Roblox Pakai GG Game Space
  • Sering Stuck Saat Registrasi Dapodik 2026.b? Coba Cara Offline Ini, Dijamin Lancar!
  • Belum Tahu? Ini Trik Isi Data Internet Dapodik 2026.B Biar Validasi Aman!
  • Belum Tahu? Inilah Cara Dapat Saldo E-Wallet Cuma Modal Tidur di Sleep Time Tracker
  • Padahal Negara Maju, Kenapa Selandia Baru Nggak Bangun Jembatan Antar Pulau? Ini Alasannya!
  • Nonton Drama Bisa Dapat 1 Juta? Cek Dulu Fakta dan Bukti Penarikan Aplikasi Gold Drama Ini!
  • Takut Saldo Habis? Gini Cara Stop Langganan CapCut Pro Sebelum Perpanjangan Otomatis
  • Gini Caranya Hilangkan Invalid Peserta Didik di Dapodik 2026 B Tanpa Ribet, Cuma Sekali Klik!
  • Rombel Hilang di Dapodik 2026 B? Tenang, Gini Cara Mudah Mengatasinya Tanpa Menu Aksi!
  • Pusing Lihat Ratusan Invalid Sarpras di Dapodik 2026 B? Tenang, Ini Cara Membereskan Datanya
  • Validasi Merah Terus? Ini Cara Tuntas Isi Data Listrik & Internet di Dapodik 2026 B
  • Inilah Trik Install Dapodik 2026.B Tanpa Patch, Wajib Uninstall Versi Lama!
  • Apakah APK PinjamAja Penipu?
  • Ini Trik Cepat Cuan di Clear Blast Tanpa Undang Teman
  • Belum Tahu? Inilah Suku Bajau Punya Gen “Mutan” Mirip Fishman One Piece, Ini Faktanya!
  • Inilah Paket PLTS Hybrid 6kVA Aspro DML 600 yang Paling Powerful!
  • Suku Tsaatan: Suku Mongolia Penggembala Rusa Kutub
  • Game Happy Rush Terbukti Membayar atau Cuma Scam Iklan?
  • Cara Nonton Drama Dapat Duit di Free Flick, Tapi Awas Jangan Sampai Tertipu Saldo Jutaan!
  • APK Pinjol Rajindompet Penipu? Ini Review Aslinya
  • Keganggu Iklan Pop-Up Indosat Pas Main Game? Ini Trik Ampuh Matikannya!
  • NVIDIA Rubin Explained: The 6-Chip Supercomputer That Changes Everything
  • What is OpenEverest? The Future of Database Management on Kubernetes
  • T3g: Code is Cheap Now, Software Isn’t
  • Is the New $130 Raspberry Pi AI Hat+ 2 Worth Your Allowance? A Detailed Review
  • Create AI Voices on Your CPU: Pocket TTS Explained for Beginners
  • Apa itu State Space Models (SSM) dalam AI?
  • Begini Cara Mencegah Output Agen AI Melenceng Menggunakan Task Guardrails di CrewAI
  • Tutorial AI Lengkap Strategi Indexing RAG
  • Cara Membuat AI Voice Agent Cerdas untuk Layanan Pelanggan Menggunakan Vapi
  • Inilah Cara Belajar Cepat Model Context Protocol (MCP) Lewat 7 Proyek Open Source Terbaik
  • Apa itu ToneShell? Backdoor atau Malware Biasa?
  • Apa itu Parrot OS 7? Ini Review dan Update Terbesarnya
  • Clipper Malware? Ini Pengertian dan Bahaya yang Mengintai Kalian
  • Kronologi Serangan Gentlemen Ransomware di Oltenia Energy
  • Apa itu CVE-2020-12812? Ini Penjelasan Celah Keamanan Fortinet FortiOS 2FA yang Masih Bahaya
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

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