Web Design Technologies — Modules 6 and 7

Site URL

https://tamato.infinityfree.io

PHP Functions and Strings!

Strings and functions are pretty closely connected. Functions can create, validate, modify, and display strings. A few examples of built-in string functions are strlen(), this gives you the length of a string which is especially helpful when validating password length, there is also strtoupper() to make the string all uppercase characters; strtolower() to make all lowercase characters. You can use echo to print out a message to a human readable format. You have to concatenate words with a ” . “.

This week I had a good time exploring PHP. It reminded me of my intermediate programming class where we were coding in Java. Don’t get me wrong, PHP is very different from java, but writing the function portion for this week reminded me of writing functions in Java. For my function I made a basic price calculator for a fixed dollar amount, and percentage off. Then the program printed the dollar amount, percentage off, and the final calculation. Below is my function:

<?php
function calculateDiscount($price, $discountPercent) {
$discountAmount = $price * ($discountPercent / 100);
$finalPrice = $price - $discountAmount;
return $finalPrice;
}
$price = 80;
$discount = 15;
$result = calculateDiscount($price, $discount);
echo "Original Price: $" . $price . "<br>";
echo "Discount: " . $discount . "%<br>";
echo "Final Price: $" . $result;
?>

For the string portion I wrote some basic text, and had used a few built-in functions to alter the text, then I had it return the length of the string as well. Below is my string code

<?php
$example = "PHP is fun!";
echo "Normal: " . $example;
echo "<br><br>";
echo "lowercase: " . strtolower($example);
echo "<br><br>";
echo "UPPERCASE: " . strtoupper($example);
echo "<br><br>";
echo "String length: " . strlen($example);
?>

All in all this was a pretty fun week exploring PHP. I am excited about next week learning how to integrate PHP with forms!

Response

  1. IT Insights Avatar

    The article discusses PHP functions and strings, highlighting how they are interconnected and useful for creating, validating, modifying, and displaying strings. This information is valuable for web developers working with PHP.

    Like

Leave a Reply