Showing posts with label learn php. Show all posts
Showing posts with label learn php. Show all posts

03 January 2011

Php For Beginners (Lesson 3)

Overview

In this lesson we will be discussing strings in php and some common and useful functions and operations performed on strings. So what is a string variable? Basically a string variable is just an ordinary variable except that it stores text in it.

Example
< ?php
$test=”This is a test string variable which stores text values.”;
echo $test; // outputs “This is a test string variable which stores text values.”
?>

Concatenation

If you want to merge two string variables or you want to append some text in an existing variable then you need to use concatenation operator (.).

Example # 1
< ?php
$first_variable=”Hello ”;
$second_variable=”world”;
echo $first_variable.$second_variable; // outputs “Hello world”
?>

In Example 1 we are concatenating two string variables. So the output will be the concatenated value i.e “Hello world”.

Example # 2
< ?php
$first_variable=”Hello ”;
echo $first_variable.” world”; // outputs “Hello world”
?>

In example 2 we are appending some text in an existing variable. So the output will be “Hello world”.

Str_replace() Function

Str_replace() is one of the most important functions used in manipulating string variables. This function is used to find and replace any piece of text.

Syntax

str_replace(string to be searched ,string to be replaced with,subject variable);

Example
< ?php
$first_variable=”Hello world”;
str_replace(“world”,”guys”,$first_variable);
echo $first_variable; // outputs “Hello guys”
?>

In above example we replaced “world” with “guys” so the output was “Hello guys”.
Strstr() Function

Strstr() is another useful function for manipulating string variables. This function is used to check whether a piece of text exist in a string variable or not. Strstr() returns false if searched string is not fount and if it is found then it will return the rest of the string from the last occurrence of searched string.

Syntax

strstr(subject variable,string to be searched);

Example
< ?php
$first_variable=”My email address is test@example.com”;
$check=strstr($first_variable,”@”); // outputs “@example.com”
?>

Strlen() Function

Strlen() function returns the length of the given string variable or string. This function comes in handy in many situations where you need to find out exactly how many characters a string contains.

Syntax

Strlen(string str);

Example
< ?php
$first_variable=”My email address is test@example.com”;
echo strlen($first_variable); // outputs “36”
?>

In the above example we tried to find out the length of a string variable so the output was 36 i.e. number of characters in that variable including spaces were 36.
Strpos() Function

This function is used to find out the exact position at which a certain piece of text or a character is located in the given string variable or string.

Syntax

Strops(subject,string to be searched);

Example
< ?php
$first_variable=”My email address is test@example.com”;
echo strpos($first_variable,”@”); // outputs “24”
?>

In the above example we tried to find out the location of “@” in the given string. So the output was 24 i.e. exact position of “@” in the given string variable.

References

http://www.php.net
http://www.w3schools.com

next Previous Lesson --- Next Lesson next

29 December 2010

Php For Beginners (Lesson 1)

Definition
PHP: Hypertext Preprocessor is a widely used, general-purpose server side scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document.

Installation
In order to run php on your own machine you need to install following three things:
  • Apache Server
  • Php
  • MySQL
But to make this lesson easier I will not discuss explicit installation of all the three things, rather I would tell you guys a simpler way of doing this. For this purpose we can simply use WAMP/XAMPP Server which is an all in one package of apache, php & MySQL.

WAMP Server can be downloaded from the link below:
http://sourceforge.net/projects/wampserver/files/WampServer%202/WampServer%202.0/WampServer2.0i.exe/download

Or you can download XAMPP Server for windows from the below mentioned link:
http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/xampp-win32-1.7.3.exe/download

After downloading simply follow the steps to install WAMP/XAMPP server on your PC. Once the installation is complete you can test it by typing http://localhost/ in the explorer. This should open a default page for the WAMP/XAMPP Server. If the default page does not come up then try restarting the WAMP/XAMPP Server.

How to Start
Now that php is successfully installed on your PC, we need to know where you should type the php code. Well you can use Notepad, Dreamweaver or any other editor to write in the php code. I would recommend that you use Dreamweaver because it’s a lot easier to use.

After writing the code, you need to safe the file with a .php extension. For example you can save your file as test.php in the respective server folder.

In case of WAMP Server you need to save your php files in wamp > www directory. The exact path of this directory is:
C:\wamp\www\test.php

Whereas if you are using XAMPP Server then you need to safe your php files in the XAMPP > htdocs directory. The exact path should be:
C:\xampp\htdocs\test.php

Now you can run your php file by typing http://localhost/test.php in your browser.

Syntax
A Php code will always be encapsulated between "<?php " and "?>" each instruction will be terminated by semicolon (;).

Example
<?php
some php code....;
some php code....;
some php code....;
?>
Hello World Example
In php if you want to display something on the browser’s screen, you need to use “echo” function.

Syntax
echo “anything you like to display on the screen”;

Example
<?php 
echo "Hello World!";
?> 
Ok guys that is all for the first lesson now you should practice and play with this code to get used to with the php syntax.

References
http://www.wikipedia.com
http://www.w3schools.com 


Next Lesson next