Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

Text & Number Management Functions

pages (4): 1 2 3 [4]


Number Manipulation
Well, now that we’ve finished hashing let’s move on to some basic math. PHP has logical operators built in to help you with math, so let’s have a look at some:

Addition: [num] + [num]
Subtraction: [num] - [num]
Multiplication: [num] * [num]
Division: [num] / [num]
Modulus: [num] % [num]

What is modulus? Well, it returns the remainder of num1 divided by num2. I.e. the modulus of 5 and 2 is 1. 5 / 2 = 2r1. Remainder: 1.

For iteration looping, if you want to add one to your iteration identifier, you can use '$i++' instead of '$i += 1' or '$i = $i + 1'. Similarly, if you want to take one off, you can use '$i--'.

Here’s an example:

<?php

$num1 = 5;
$num2 = 2;

$add = $num1 + $num2; # 7
$sub = $num1 - $num2; # 3
$mul = $num1 * $num2; # 10
$div = $num1 / $num2; # 2.5
$mod = $num1 % $num2; # 1

?>

Finally, I’ll cover exponents and square roots. PHP has built in functions to deal with each of these. They are: pow() and sqrt(). Let’s look at pow() first. It accepts the base number and the exponent and is pretty straight forward. Example: pow(2,2) returns 4, pow(3,3) returns 27.

sqrt() accepts one argument only: the base number. sqrt(4) returns 2. sqrt(9) returns 3 etc. If your number is negative, it is an imaginary number, and PHP gives you a nice little error message, as it does if you input 0. To calculate the square root of negative numbers, square root the abs() (positive) value of your number and slap an “i” on the end of it. E.g. sqrt(-9) would return an error, but if you use '$num=sqrt(abs(-9))."i"' then it will return “3i”. For more information about i and imaginary numbers, Google it!

sqrt() can be replicated by the following: pow([num], 0.5). A number to the power of 0.5 is the same as the square root of that number.

Anyway, enough of that! Onwards to Padding strings!

Padding Strings
This will be a short section, as it only uses one function: str_pad(). Going allllll the way back to the first section, let’s imagine that our administrator wants to take somebody’s name inputted via a form, and if it’s not x characters long, add spaces or dashes or something to make it that long. This is the main function of str_pad().

It accepts these arguments: [input], [pad length], [pad], [where]. [Input] is the inputted string, [pad length] is the length you want your string to be, [pad] is the string of what you will pad it with (it can be numbers, letters, anything), and [where] specifies where to put the padding. If you don’t specify anything, it puts it to the right. You can put either of the following values in there:

STR_PAD_RIGHT
STR_PAD_LEFT
STR_PAD_BOTH


So, if you put in STR_PAD_RIGHT it will put the padding on the right of the existent string. STR_PAD_BOTH will divide the necessary number of added pads by 2 and add half to each side.

Converting Text Classes
We are nearing the end of this article! This section is also reasonably short, so I’m going to outline 4 PHP functions that change character cases:

strtolower() takes a string, and converts it to lowercase.

strtoupper() takes a string and converts it to uppercase.

ucwords() takes a string and converts the first letter of each word to uppercase. If it’s already uppercase it leaves it alone.

ucfirst() takes a string and converts only the first letter of the whole string to uppercase. Once again it leaves it alone if it’s already uppercase.

They all allow one string of input, containing the text to be formatted. So, let’s write an example!

<?php

$text = "scrowler LIKES apples";

$lo = strtolower($text); # scrowler likes apples
$up = strtoupper($text); # SCROWLER LIKES APPLES
$wr = ucwords($text); # Scrowler LIKES Apples
$fr = ucfirst($text); # Scrowler LIKES apples

?>

And, finally, I’ll conclude the article with a short section on word wrapping.

Word Wrapping
Since this isn’t very challenging, this section will also be short. You can do it with one function! This is useful if you need to add line breaks every x characters. The function is called wordwrap() and accepts the arguments: [string], [width], [linebreak], [cut]. We won’t worry about the cut argument at the moment.

[Width] specifies the width of the line, and [linebreak] specifies what to put at the end of the line. Use '<br />\n' to do a linedrop in both <pre> tags and in regular XHTML.

Here’s an example.

<?php

$string = "a long string that will be cut up into smaller parts.";

echo wordwrap($string, 10, "<br />\n");

?>

This will output:

a long <br />
string <br />
that will <br />
be cut up <br />
into <br />
smaller <br />
parts.

It’s that easy. Have fun! Oh, and that concludes the article! I hope you've learned as much as our database administrator! ;). If you have any questions, you can contact me via the Creative Forums - just click my name below to visit my profile where you can PM or email me.  See ya!

- Tutorial written by Scrowler

Pages (4): <Prev 1 2 3 [4]
Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments


There are no comments for this tutorial yet.
You can place a comment by clicking here.
Amazing Font Pack!

Featured Tutorialsmore

Extracting Hair
Extracting Hair
- Adobe Photoshop -
Hershey Kisses
Hershey Kisses
- Adobe Illustrator -
Metal Wires & Cables
Metal Wires & Cables
- Adobe Photoshop -
Grunge Rubber Stamp
Grunge Rubber Stamp
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 887 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1052 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1054 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1935 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1832 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14441 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2751 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2772 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3439 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2356 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2499 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2798 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673