⊗ppPmUFOP 196 of 447 menu

Optional Function Parameters in PHP

Function parameters can be made optional. To do this, parameters need to be given default values. Let's say for example we have the following function:

<?php function func($num) { echo $num ** 2; } ?>

Let's make it so that this parameter defaults to the value 0:

<?php function func($num = 0) { echo $num ** 2; } ?>

Let's test our function with a parameter:

<?php func(2); // outputs 4 ?>

Let's test our function without a parameter:

<?php func(); // outputs 0 ?>

Given the function:

<?php function func($num = 5) { echo $num * $num; } ?>

This function is called in the following way:

<?php func(2); func(3); func(); ?>

Explain what the result of each function call will be.

Given the function:

<?php function func($num1 = 0, $num2 = 0) { echo $num1 + $num2; } ?>

This function is called in the following way:

<?php func(2, 3); func(3); func(); ?>

Explain what the result of each function call will be.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline