Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with Strings, Operators and Conditional Statements in PHP

In this article we will see some functions to work with Strings in PHP. Just like Concatenate Strings, we will also talk about String Length, Position, operators and conditional statements with their usage..

Introduction:

Most of us already know what PHP is and how to install this application. Well, PHP is just a scripting language which you can use to create simple as well as complex websites. To use PHP you need some software. You will need the code editor, Apache, PHP and MySql database application.

Most of the web hostings support PHP codes. If you are practicing it at home or if you are willing to create a website to use under shared network then you will have to install PHP and Apache and MySql database. Download Apache from Apache.com and download PHP from php.net and if you want to work with database then you must install MySql in your desktop.

PHP String

Now let’s come back to main topic. You know how to post any message directly by Echo, you know how to write comments in a script and you also know how to use a variable(discussed in last post). Now we will discuss about “PHP string”. What is a string? If you have previous knowledge of C or any programming knowledge then you already know what a string is. Otherwise, here is a short description of “string” for you.

A string contains a bunch of characters in it. You may input alphabet or you may input number. You can input both together too. But you cannot add two strings. As an example, if you input two strings and if both strings are number too you cannot apply any mathematical terms . You can count the length of a string but you cannot use any mathematical terms in a string. We will know all the important function of string today.

Let's discuss a simple example:

Listing 1: PHP String

<?php
$text =”This is a string”;
echo $text;
?>

Here “this is a string” is the value of the “text” string. You can print a string and you can do many other things with string. You can also print a number.

Listing 2: PHP Showing number

<?php
$num =100;
echo $num;
?>

See the differences of strings and normal variable. To declare a normal variable which contains number, you will not have to use “…” but when you are declaring a string value, no matter if the value is a plain text or numbers, you must use “…”. You can use any mathematical terms under function with the variable but you cannot use a mathematical term to work with a string value. There is so much more differences with a string and with a normal variable.


Concatenate 2 Strings

If you want to print two strings at the same time then you have to use dot (.) example is below.

Listing 3: Concatenate Strings

<?php
$text=”The price of an apple was”;
$price=”0.5  USD”;
$completeText =  $text.” “.$price;
echo $completeText;
?>

Here the output will be >> The price of an apple was 0.5 USD

Both of the string were printed at the same time and together. There are so many more things you can do with strings.

String Length

Let’s see how to print the length of a string. If you input any string and if you want to print the length of that string you have to use strlen. Strlen stands for “string length” .

Listing 4: Concatenate with String Length

<?php
$string=”Hello”;
echo strlen($string);
?>

The out put will be >> 5 << because the total length of “Hello” is 6.

You can use strlen for several purposes. You can use strlen to find out the length of a big input and then you can compare the input with a number. You will in some website if you input more than 16 characters in name box the website will give a warning message. This thing they do by using strlen. There are more uses of strlen.

String Position

In a string if you want to check the position of a value then you have to use this strops. Let’s see an example.

Listing 6: String Position

<?php
$text=”My name is X”;
echo strops($text,”X”);
?>

The work of this function is searching and finding out the place where the inputted value is situated. If you use echo function then the position number will be printed. The example will print >> 9 << because the position of “X” is 9. You can input as long string as you want and you can find the position of a value from it.

String comparison

After strpos the next important function of string is strcomp. Strcomp stands for string comparison. You can use two strings to find if they are same by strcomp. This is the most important string function. An example usage is when you login to any website and if you input wrong password the website returns an error message and that is “wrong password”. This thing can be done by using strcomp. Keep a database of passwords and when someone gives the information if the information of your database is not same with the information that they gave then you can create an error message for them.

PHP Operators

Operators are one of the important thing in PHP. You will need to use operators many times. Some examples of operators are given below.

  • + Operator will add two variables.
  • - Operator will subtract two variables.
  • * Operator will multiply two variables.
  • / Operator will divide two variables.
  • % Operator will find out the modulus.
  • ++ Means increment
  • -- Means decrement.

There are many other operators in PHP but those are the basic operators and most used operators. Some other common operators are:

  • == means equal to. When we are comparing two variables or two strings or anything that can be compared in PHP and if both are same then we should use this operator as an example if $a variable and $b variable contains the same value then we can say that $a==$b. That means a variable is equal to the b variable.
  • != Not equal to. When two variables are not same then we should use this. As an example, if $a has value 6 and $b has value 3 then we can write that $a~=$b. By this operator we can prove many other things in PHP. Sometimes we will need to use these operators in terms. Some terms like if….else will require these operators.
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to.

Operator usage in if/else statement

The most common statement in PHP is if/else statements. You will need to use this statement a lot of time in your script. The syntax of this statement is :

Listing 7: PHP If/else Syntax

<?php
if (run some codes here){
	echo “print one”;
}else{
	echo”print two”;
}
?>

Here the first echo function will work if the codes are matched I mean if the statement in “if” is true and if the statement is false then the second echo will be printed out. You can also use more than one statements in one if…else statement.

Listing 8: PHP If/else program

<html>
<body>
<?php
	$num=”5”;
	if ($num==5){
		echo "The number is 5!";
	}else{
		echo "Sorry, the number wasn’t 5!";
}
?>
</body>
</html>
This code will output the first text “the number is 5!” because the statement in “if” is right and if the statement was wrong then the script would print the echo under “else”. Now another example of “if/else” where the second echo will be published.

Listing 9: PHP If/else program

<html>
<body>
<?php
	$num=”5”;
	if ($num==10){
		echo "The number is 5!";
	}else{
		echo "Sorry, the number wasn’t 5!";
}
?>
</body>
</html>

The script is almost same but the here the value is 10 so the code will output the second echo. This is how if/else statement works in PHP.

Now let’s learn what is switch/case statement and how to work with that. switch/case statement are used for comparison purpose. As an example, you have one value and you want to compare that value with a lot of other variables. If any of the comparison is true then the code will execute that portion of the code and will publish the result. There is an option named “break” so if any comparison is true then the code will stop and it will not check anything else ie come out of switch. The syntax of switch/case is given below:

Listing 10: PHP switch/case syntax

<?php
switch (expression){
	case value1:
	//code to be executed if expression = valuel1;
		break;
	case value2:
//code to be executed if expression = value2;
		break;
		default:
//code to be executed
//if expression is different from both value1 and value2;
}
?>

This is the syntax of switch/case statement. One sample will make you crystal clear on switch/case statement.

Listing 11: PHP switch/case program

<html>
<body>
	<?php
	switch ($num){
		case 1:
			echo "Number 1";
			break;
		case 2;	
			echo "Number 2";
			break;
		case 3:
			echo "Number 3";
			break;
		default:
			echo "No number between 1 and 3";
	}
	?>
</body>
</html>

This is a very simple code on switch/case statement and it will help you to understand how switch/case statement works. You can compare many things in the switch case statement. Switch/case stamen people basically use when someone have to find out and compare a value with a big database. If/else and switch/case works almost in the same way but it is hard to write if/else statement if there are a lot of elements to compare and if the elements are less then you should use switch/case.

This is all for this article. See you next time .



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login