09.25.07
Posted in PHP at 6:51 am by admin
PHP strlen() and strops() Functions
Now that you have a basic grasp and understanding of just how the basics of PHP string variables work, it is important to continue that information by introducing two more functions that you can use in order to perform actions on the strings that you have in your code. These functions might seem pretty useless at the moment, but keep them in mind for later when you have a greater wealth of knowledge that allows you to program more complicated PHP scripts. You will find that this knowledge might come in very handy in more than one circumstance.
The first of the two functions that will be discussed in this article is the strlen() function. Strlen stands for String Length and as the name might simply it is a function that you can use to get the PHP script to calculate the length of a particular string. For example, if we were to take our string of “PHP Rocks” from the previous examples, the strlen() function syntax would look like this:
<?php
Echo strlen(“PHP Rocks”);
?>
When this PHP script was executed on the server, the output that was returned would be the number 9. This is because there are three characters in PHP, one character in the space and five characters in the word rocks for a total sting length of 9 characters. String length is always measured in characters when using the strlen() function. This function can be useful for people that are using string variables to keep track of the number of times something has been done. For example, if a teacher wanted to see how many assignments had been given to a particular student, all they would have to do was do a simple strlen() of the string of grades the student had to get the answer they wanted.
Another useful function is the strpos() function. Strpos stands for String Position and basically signifies the ability of the script to find the exact position of a smaller string within a larger string.
For example, if we wanted to find the position of the word Rocks in the string PHP Rocks, we would execute the following script:
<?php
Echo strpos(“PHP Rocks”,”Rocks”);
?>
The first position in the string is always 0 rather than 1 and therefore the output returned by this script would be the number 4. This is because the first part of the Rocks string is five places into the larger string PHP Rocks.
Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.
Permalink
09.24.07
Posted in PHP at 12:05 pm by admin
Understanding PHP String Variables
If you are interested in truly coming to grips with the awesome power of PHP, then one of the things that you are going to have to do is learn how variable systems within the programming language work. These variable systems allow you to execute the most complicated of PHP scripts and if you learn them at the start of your PHP training then it will be much easier to figure out exactly what to do about them later.
In order to understand how variables work and what the appropriate syntax for variables is, let us look at the example of PHP code that we produced in the article that discussed PHP being a loosely typed language:
<?php
$text=”PHP Rocks”;
$number=1
?>
While this is most certainly a solid piece of code, you will notice that the variable $number is not a string value but rather an integer value. So let’s get rid of that for a moment and add the echo command to the PHP script so that we can actually get it to execute something.
<?php
$text=”PHP Rocks”;
Echo $text;
?>
If you were to execute the above script on your server, the output would simply be the phrase PHP Rocks; the same as if you were to simply type the phrase PHP Rocks into your actual HTML file. This might seem a bit simplistic, but later on when you are programming exceptionally complicated PHP scripts, you are going to need to know this information by heart. To finish off the lesson, let’s add the $number variable back in but make it a string be adding three more digits to the value.
<?php
$text=”PHP Rocks”;
$number=”1111”;
?>
Now, if we want to put these two strings together it requires the use of the concatenation operator, which has the syntax of a period in PHP.
<?php
$text=”PHP Rocks”;
$number=”1111”;
Echo $text.$number
?>
This will produce the following output: PHP Rocks1111
If you want to separate the two strings with a space, then you need to actually concatenate the space into the actual PHP script:
<?php
$text=”PHP Rocks”;
$number=”1111”;
Echo $text.” ”.$number
?>
This will produce the following output: PHP Rocks 1111
In that last example, the quotation marks surround a single space, which is concatenated to the two string variables through the use of two concatenation operators. When you see PHP scripts capable of executing in such a way that the output is long string values, then chances are there are quite a few concatenation signs involved in that output.
Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.
Permalink
09.22.07
Posted in Blog, PHP at 11:19 am by admin
Logical Operators Used in PHP
In a previous article, two important groups of operators were covered. The arithmetic group of operators which consisted of obtaining a number result through the manipulation of two or more previous numbers and the comparison group of operators which were used to compare one number to another in a variety of different ways. There is a third and all important group of operators known as the logical operators and indeed regardless of the type of programming that you do you are probably going to have to use logical operators at some point. This is why they have been given their own article, so that each of the three can be explored deeper.
The first of the three is the && operator, which simply stands for the word and. If you want more than one thing to be signified in any part of any script that you write, you are going to use the && logical operator.
Here is an example:
if x = 1
and y = 2
then (x<2 && y<2) would return false, but (x<2 && y<3) would return true.
Another operator that you need to concern yourself with is the || operator. This symbol stands for the word or and just like and it is used in a number of different places within the PHP script. If you want one of a group of things to be selected or to be true but you don’t necessarily require the others in the same group to also be selected or true, then you would use the or operator instead of the and operator.
Here is an example:
If x = 1
And y = 2
Then (x<2 || y<2) would return true instead of false as was the case in the previous example where the && operator was used.
The third operator of importance from the logical standpoint is the ! operator, which stands for the word not. Not is used in a somewhat different way than the other two logical operators because you are specifically using it when you want something to either not be correct or alternatively not be chosen.
Here is an example:
If x = 1
And y = 2
Then !(x==y) would return true, but !(x!=y) would return false. Notice that the second example returns false because of the double negative that exists in the statement. If x is not equal to y, then the statement becomes x is equal to y. Since x is not equal to y, the statement is false.
Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.
Permalink
Posted in PHP at 11:18 am by admin
One of the things that programmers detest with a passion about scripting is the fact that a lot of scripting languages require absolute rigidity when it comes to everything that’s done in a language. This means that unless you are very confident of yourself, simply coding in something like notepad is not going to be a good idea for the simple reason that if you miss a period or other punctuation mark, you are going to have to manually search through everything in order to find it.
However, PHP is a bit different in that sense. While there is a very clear syntax when it comes to the basic PHP commands, at the same time what you are going to find is that PHP is a loosely typed language. This means that there are a lot of things that you can do that infer something that the PHP script will execute for you. This might seem like bit of a foreign concept, so we will illustrate it further through the use of variables.
Take the following piece of example code:
<?php
$text = “PHP Rocks”;
$number = 1;
?>
This illustrates the basic $variablename = variablevalue; syntax that is used when declaring variables, but at the same time also illustrates that text or number strings need to be enclosed within quotation marks whilst non-string values do not. These are all rigid aspects of the PHP language, but if you are familiar with other forms of programming you might have already noticed an important difference between PHP and many other programming forms.
In completely rigid programming languages, you need to specify both the name of the variable and the type of the variable before you can use it later on. Examples of such languages include the C group of languages as well as older languages like Turbo Pascal. In PHP however, you only need to signify the value and the script will automatically infer the appropriate type for you. This is one of the things that make PHP a loosely typed language.
There are of course other things that make PHP a loosely typed language, but they are similar examples to what has been illustrated over the course of this article. The PHP variables are useful in executing other parts of scripts and because of the fact that you don’t need to signify both the name and the type of the variable beforehand, you can save some space in your programming software.
Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.
Permalink
Posted in PHP at 11:17 am by admin
In the PHP scripting language, there are three primary kinds of operators; arithmetic operators, comparison operators and logical operators. Depending on the type of scripting that you do, you are more likely to use one group than the other two. However, this article provides a brief précis of two of the three types so that you know generally what to do should that situation arise in one of your scripts.
Arithmetic
There are seven primary arithmetic operators that can be used in PHP scripting.
The first one is the addition operator, symbolized by the + symbol. If you designate a variable x to be equal to one and then add the x+1 operation into your script, the number 2 will be returned. The same operational arithmetic is used in the subtraction (-), multiplication (*) and division (/) operators.
The fifth operator is the modulus operator, symbolized by the % symbol. It is expressed in the form of x%y, with both x and y being numbers. It basically returns the remainder when x is divided by y, so for example the script of 5%2 would return a result of 1, because one is the remainder when 5 is divided by 2.
The increment (++) and decrement (–) operators are used to increase or decrease the number in a variable by one. For example, if x=5 and you place x++ down into the script, the value six will be returned. Likewise, x—would return a value of four.
Comparison
Comparison operators, as the name might imply, are used to compare one number to another. They consist of equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=) and less than or equal to (<=). At both ends of the operator being used, numbers are placed. Based on the numbers and the operator being used, the output that is returned is either the word ‘true’ or the word ‘false’.
For example, if the statement is 1==2 (1 is equal to 2), then the output that was returned would be false. Likewise, if the statement is 1!=2 (1 is not equal to 2), then the output that was returned would be true. These types of operators might not seem important right now, but like most of the PHP fundamentals that you end up learning they are going to get more important as the PHP scripts that you design start to take on more complicated parts.
Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.
Permalink
09.13.07
Posted in PHP at 11:53 am by admin
Before you can go on and plan things out in the style of PHP, you need to understand how the basic syntax of PHP works. It is difficult to start right at the end without having the fundamentals in place and ultimately this is why you need to know for sure exactly how PHP syntax works before you go ahead into creating more complicated scripts.
Placement, Starting and Ending
You can use PHP scripting within your HTML files by using the proper syntax to denote the PHP script. PHP scripts always open with “<?php” and always close with “?>”.
In other words, the following would be the correct way to right a PHP script:
<?php echo “This is a Test”; ?>
The following PHP script would tell the server to print the phrase This is a Test and because the basic syntax of opening and closing is correctly placed, the script would execute perfectly.
However, if you were to structure your PHP script like this:
<php echo “This is a Test”; ?>
People that visit your website would end up seeing an error on the page because of your improper syntax within the PHP script. Remember that proper syntax is the key to making things work and this is as true for PHP as it is for HTML or any other web-based or programming-based language.
Commenting
Whether you are building the script for someone else or are just interested in documenting your progress so that you remember what you were thinking about later, PHP commenting can come in quite useful for either purpose. PHP commenting is done differently from HTML commenting however, with single-line comments denoted by their starting with “//” and multiple-line comments being bounded above by “/*” and below by “*/”
For example:
<?php
//Single-line comment
/*
Multiple
Line
Comment
*/
?>
This PHP script would actually not return anything in terms of viewable material by a person visiting your website for the simple reason that all it has within it are two comments.
Of course, if you wanted to combine the two ideas:
<?php
//single-line comment
Echo “This is a Test”;
/*
Multiple
Line
Comment
*/
?>
The end result of this PHP code would once again be the printing of the phrase This is a Test. The two comments would not show up because comments in any programming language do not affect the output that language creates; they are merely there for the author to remark on a particular piece of code or alternatively for multiple authors of the same code to exchange information.
Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.
Permalink