| 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
| 
There are no comments for this tutorial yet. You can place a comment by clicking here.
|

|
|
 |
Auto contrast question Author: Gjbphp Posted: Oct 06th, 6:55pm Activity: 1 replies, 10 views
|  | Calling All PS7 Users... Author: tamlin Posted: Oct 06th, 6:17pm Activity: 0 replies, 11 views
|  | Qmark Database - Photoshop site! Author: Rhamises Posted: Oct 06th, 4:09pm Activity: 0 replies, 16 views
|  | hello!!Mabuhay!! Author: Icekimo_2000 Posted: Oct 06th, 3:32pm Activity: 3 replies, 22 views
|  | PHP - Pagination (Whats Wrong?) Author: red123 Posted: Oct 06th, 3:05pm Activity: 0 replies, 10 views
|  | Question about rar.documents ... Author: Meeps Posted: Oct 05th, 9:25pm Activity: 5 replies, 55 views
|  | Gigapan... gotta have one! Author: synthetic Posted: Oct 05th, 4:18am Activity: 1 replies, 59 views
|  | Dodgy Ad Reporting Author: Man1c M0g Posted: Oct 03rd, 9:21pm Activity: 5 replies, 98 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 210 |
| Total Downloads: | 413 |
| Linkbase Links: | 255 |
 |
|
 |
 |
|