Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

INFO-1208: PHP FUNDAMENTALS – LAB 10

Complete each exercises below to demonstrate your knowledge of functions. For each exercise,
screenshot the code and output side by side.

Exercise 1: Basic Function


1. Create a PHP file named "basic_function.php."
2. Write a function named `simpleFunction` that echoes "Hello, world!" when called.
3. Call the `simpleFunction` and run the PHP script.
4. Take a screenshot of the code and the output.

Exercise 2: Function with Parameters


1. Create a PHP file named "function_with_parameters.php."
2. Write a function named `getCost` that accepts two parameters: `$qty` and `$price`, and echoes
the product of these two values.
3. Call the `getCost` function with different values for `$qty` and `$price`.
4. Take a screenshot of the code and the output.

Exercise 3: Function with Return Value


1. Create a PHP file named "function_with_return.php."
2. Rewrite the `getCost` function to return the product of `$qty` and `$price` instead of echoing
it.
3. Call the `getCost` function and store the result in a variable.
4. Display the result using `echo`.
5. Take a screenshot of the code and the output.

Exercise 4: Default Parameters


1. Create a PHP file named "default_parameters.php."
2. Modify the `getCost` function to have a default value of 5 for `$price`.
3. Call the function with only the `$qty` parameter and with both parameters.
4. Take a screenshot of the code and both outputs.

Exercise 5: Named Arguments (For PHP 8 and Later)


1. Create a PHP file named "named_arguments.php."
2. Rewrite the `getCost` function using named arguments and call it using named arguments.
3. Take a screenshot of the code and the output.

Exercise 6: Type Hinting


1. Create a PHP file named "type_hinting.php."
2. Rewrite the `getCost` function to include type hints for `$qty` and `$price` as integers.
3. Call the function with the correct data types.
4. Take a screenshot of the code and the output.

Exercise 7: Void Return Type


1. Create a PHP file named "void_return_type.php."
2. Create a function named `displayCost` that accepts two integers and echoes the product.
3. Update the function to have a return type of `void`.
4. Call the function and take a screenshot of the code and the output.

Exercise 8: Union Types


1. Create a PHP file named "union_types.php."
2. Rewrite the `getCost` function to accept and return integers or floats using union types.
3. Call the function with different data types.
4. Take a screenshot of the code and the output.

Submit Your Lab


Place the following files in a folder:
• Screenshots from Exercises 1 through 8 (8 screenshots total). Please name your files
accordingly!

Compress the folder to create a single .zip file. Rename your .zip file to “info1208-lab10-
<yourusername>.zip”, replacing “<yourusername> with your FOL username.

Submit your .zip file to the Lab 9 Dropbox in FOL, found under Evaluations > Submissions >
Lab 9. You can submit your lab as many times as you need, but each submission will overwrite
the last.

Rubric:

Description Marks Available


Parts 1 through 8: 9
- All code is valid and achieves the proper purpose of the function
- Screenshots include code and output
All naming conventions and instructions followed. 1
All code is valid HTML and/or PHP.
TOTAL: 10

The use of any generative AI (e.g. ChatGPT, etc.) is strictly forbidden. Use of such AI on these
assignments will result in a mark of a zero and will count as an academic offence.

You might also like