Skip to Content
プログラミングPHP 第4版
book

プログラミングPHP 第4版

by Kevin Tatroe, Peter MacIntyre, 永井 勝則, 中野 圭
April 2023
Intermediate to advanced
456 pages
6h 11m
Japanese
O'Reilly Japan, Inc.
Content preview from プログラミングPHP 第4版

3章関数

 関数は、何らかのタスク(作業)を実行する名前付きのコードブロックを言い、与えられた値のセット(パラメータと呼ばれます)に基づいて動作したり、単一の値や配列に入れた値のセットを返すこともあります。関数は、何回呼び出されても、ページで1回コンパイルされるだけなので、コンパイル時間を節約します。またバグの修正は、タスクを実行する箇所すべてでなく、1箇所で済ませることができるので、コードへの信頼性が上がり、特定のタスクを実行するコードを分離するため、可読性も向上します。

 本章では、関数呼び出しと関数定義の構文を紹介し、関数内の変数を管理する方法や関数への値の渡し方(値渡しと参照渡し)について見ていきます。また、可変関数や無名関数も取り上げます。

3.1 関数の呼び出し

 PHPプログラムの関数には、組み込み関数(または拡張モジュールで導入される事実上の組み込み関数)やユーザ定義関数があります。どの場合も、同じように評価されます。

$someValue = function_name( [ parameter, ...] );

 関数が必要とするパラメータの数は、関数によって異なります(後述するように、同じ関数でも数が異なることもあります)。関数に渡されるパラメータは、有効な式なら何でもかまいませんが、順番は関数の求める順番でなくてはいけません。順番が変わっても、関数は動作するかもしれませんがそれはたまたまで、基本的には「ガベージイン、ガベージアウト」*1です。関数のドキュメンテーションを読むと、その関数に渡すべきパラメータは何で、どんな値が返ってくるかがわかります。

*1 [監訳注]ゴミを入れれば、ゴミが出てくる。つまり、おかしな入力を与えると、おかしな出力しか得られない、ということ。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

プロダクトマネジメント ―ビルドトラップを避け顧客に価値を届ける

プロダクトマネジメント ―ビルドトラップを避け顧客に価値を届ける

Melissa Perri, 吉羽 龍太郎

Publisher Resources

ISBN: 9784814400270Other