Free Online Articles Directory
18.11.2008 Sign In Register Hello Guest
Email:
Password:
Remember Me 
forgot your password?


Self-taught Php: String Functions Quick Start

Author: Bill Hamilton Author Ranking Blue | Posted: 11-06-2008 | Comments: 1 | Views: 306 | Rating:  (93) Article Popularity - Blue (?) Got a Question? Ask.
Sign Up Now!

Self-Taught PHP - String Functions Quick Start


by Bill Hamilton


An on-the-fly kind of programmer looking at an unfamiliar lanquage needs first of all a reference of the set of necessary and commonly used functions. This tutorial provides a quick reference of the essential string functions followed by some condensed notes and then a very brief list of PHP operators and process control.


Functions

substr($S,start,length)                     portion of string S from start with length
strstr($S,$lookfor)                             first part of string S, up to string lookfor
strstr($S,$lookfor,True)                    last part of string S, beginning with string lookfor
strrchr($S,$lookfor)                           last part of string S, beginning with last character lookfor
strpos($S,$lookfor)                          first position of string lookfor in string S
strrpos($S,$lookfor)                           last position of string lookfor in string S
strlen($S)                                       Length of string S
chr(ascii)                                         Character specified by ascii code
substr_replace($S,$R,s,L)              Put $R into $S, beginning at position s (optional length L)
str_replace($F,$R,$S)                   find and replace string $F in string $S, replacing with $R
trim($S)                                         strip whitespaces from end and beginning of $S (also ltrim and rtrim)
$S.$T                                                 concantentate strings, place string $T at the end of $S
echo "hello world"                            writes "hello world"


Here are some translation examples for Visual Basic programmers:

Basic PHP Returns

right("abcde",3) substr("abcde",-3) "cde"
left("abcde",3) substr("abcde",0,3) "abc"
mid("abcde",3,2) substr("abcde",2,2) "cd" (count starts at 0)
len("abcde") strlen("abcde") 5
replace("abcd","bc","XY") str_replace("bc","XY","abcd") "aYXd"
instr("abcd","c") strpos("abcd","c") 2 (starts at 0)
chr(27) chr(27) escape character
ltrim(" abc") ltrim(" abc") "abc"


These are the basic string functions you expect of a scripting lanquage - finding a substring, replacing portions of a string, and breaking a string up. All of the indexes into the string are zero-based (the first character is at position 0). A handy feature to keep in mind is that the starting positions can be negative, which means you count from the end of the string rather than the beginning. The final necessary function is combining strings, which in PHP is simply a period operator.


Process Control


PHP implements a very basic set of process control statements, which behave in the usual fashion. They are the familiar If statement, While loop, For loop, Case block (switch), the Foreach loop, and the break and continue.


The syntax is a little different. Where I have {code} below it means any line or lines of PHP code including the curly brackets. Each line must end with a semicolon ;. The brackets group the lines into a unit. For example, the {code} after the If statement or While statement can be either a single line with a semicolon without brackets, or one or more lines with brackets. The continue command which skips the rest of the current loop iteration, has an added feature: add a number to skip the rest of nested loops. For example, continue 2 skips the current iteration and also a loop enclosing the current loop.


if ($A=="value") {code} If statement to execute the bracketed code. Use === three equals to test boolean values


while ($A=="value"){code} While loop executing the bracketed code


for ($i = 1; $i <= 10; $i++) {code} The for loop is just like in C. Increment $i and iterate

switch($i){ Case statement, just like a series of "If" statements
case 0:{code} if $i==0, execute this code
break; except that you need "break" or it will continue to execute the next lines.
case 1:{code}
default:
{code}
}
continue; skips to the end of the current loop (including a switch statement)


foreach ($A as $V) {code} when $A is an array, loops through the array for each value $V assigning the value to $V on each iteration


foreach($A as $key=>$V) if you need to access the array element and not just the value,
{code} $key is a variable for the element of $A array, $V is the value


include 'scriptfilename' inserts the contents of a file into code


The Arithmetic, logical and boolean operators are exactly as one would expect and listed below to round out this reference:


Arithmetic
-$a negative of $a
$a + $b add
$a * $b multiply
$a / $b divide (float value unless both are integer)
$a % $b remainder

Operators
&& logical AND bitwise and is &
|| logical OR bitwise or is |
! logical NOT bitwise not is ~
xor logical XOR bitwise xor is ^


 


Bill Hamilton is a former Database Administrator for United News and Media, and VNU inc. He currently operates several php/mysql driven websites including www.mysticgemcreations.com

Rate this Article: Current: 5 / 5 stars - 2 vote(s).

Article Source: http://www.articlesbase.com/programming-articles/selftaught-php-string-functions-quick-start-445895.html

Print this Article Print article   Email to a Friend Send to friend   Publish this Article on your Website Publish this Article   Send Author Feedback Author feedback  
Bill HamiltonAbout the Author:

Bill Hamilton is a former Database Administrator for VNU inc. and United News and Media, Network Systems Engineer, and Application Developer. He owns and operates several websites including Gems, Beads and Crystals

Submitting articles has become one of the most popular means to drive traffic to your website and promote yourself and your business. Join us today - It's Free!

Article Comments

Comment on this article Comment on this article
Your Name
Your Email:
Comment Body
Enter Validation Code: Captcha

0
1. Andy (05:46, 10.07.2008)
Excellent article, perfect for a VB programmer like myself moving to PHP :-)

Related Articles

Turning Visitors Into Customers
By: Peter De Pradines | 07/05/2008 | Web Design
The world has changed. Today 70% of all companies and services are originally found by way of the Internet. Tomorrow is will be closer to 95%. Will they yours be found?

Latest Web Development Features for Zend Framework
By: Osd India | 01/09/2008 | Software
Latest Web Development Features for Zend Framework, About Zend Framework, Advantages of using Zend Framework, Zend framework based development / customization / solution.

Marketing Research Tips: Gathering Requirements
By: James Brausch | 09/10/2006 | Affiliate Programs
I never gather requirements for a new product, but I always gather requirements by asking for feedback for an existing product that I am about to update. I create new products without requirements as a baseline.

Precautions for Software Outsourcing
By: Edward M. Brancheau | 16/01/2007 | Outsourcing
Software outsourcing does not always compromise quality. In fact in many cases software outsourcing is not only the most affordable option but also provides the most qualified candidates. One way to avoid the pitfalls of having quality compromised by software outsourcing

Php Vs .net
By: Nitin Tripathi | 09/10/2007 | Technology
One of the most confusing decisions that IT policy makers of companies come across is which way to go .NET or PHP. Both have their merits and limitations. Deciding one is entirely dependant upon your budget, technical skills, requirements and your overall corporate methodology.

New Futures to Website Design - Development - Hosting - SEO
By: Suresh Nair | 04/02/2008 | SEO
This article is all about website development, optimization and web hosting features. Find great job opportunities in SEO, Programming and other IT fields. Learn to create information-rich site using latest programming scripts.

Programming With PHP the Right Way
By: Jon Caldwell | 30/06/2008 | Computers
To check if installation is okay, there are 2 execution interfaces, CLI Command Line Interface and CGI Common Gateway Interface. However, if you installed PHP in the \php directory, you can 1st try to check there. Run "\php\php -v" command to check the Command Line Interface (CLI). Run "\php\php-cgi -v" command to...

People Still Cling To Their Unappreciated Cars
By: Daniel Millions | 09/08/2008 | Cars
Some people become emotionally attached to their cars because of sentimental value.

Got a Question? Ask.

Ask the community a question about this article:

Frequently Asked Questions

Parsing HTML with PHP
By: Daniel | 25-09-2008
Hi, What's the simplest way of parsing an HTML file using PHP? Thanks!

How to retrieve data on the data table?
By: Evi | 03-09-2008
How to retrieve data on the data table?

How to create a slider custom control in C# ?
By: David | 01-09-2008
I would like to know how to create a slider custom control in C Sharp, using subclassing. I just don't know where to start. I have the graphics for the background and thum. I need some code to do this, Please Help..  

Is there someone that can change my working ...
By: jazzman | 22-08-2008
Is there someone that can change my working program without charging me an arm and a leg like consultants do?

HTML training
By: knabo | 19-08-2008
What is the best way or place to learn HTML?

Where can I find a stock market simulator?
By: tomersh | 17-08-2008
I'm looking for a software/ web service that has the abilities to simulate the NY stock market in the past (not only opening and closing rates) for day trading simulation. It must also has an API or some kind of scripting language in order to issue automated requests during the sim.

Q&A Powered by:
Powered by Yedda 

Latest Programming Articles

Cool Desktop Wallpapers
By: Danny | 18/11/2008
Cool desktop wallpaper is accomplishments arrangement that displayed in the computer operating system. The wallpapers usually be acclimated in JPEG, BMP and GIF book formats. That wallpaper can be acclimated with Microsoft Windows, Linux and Macintosh Mac OS. Each adviser can be altered requirements, alike admitting wallpaper images advised for accepted monitors can be scaled up or bottomward to the fit size. Those are accessible on the internet for free. Some categories of wallpapers are a

Tips for Buying Software Online
By: Daniel Jowssey | 17/11/2008
Buying software online not only helps save the planet, it also has other benefits, including: * Ease and Simplicity. You can purchase software in your underwear at 4am if you really want to. Shopping online doesn’t have to be done within regular business hours, nor do you need to look your best to do it. It’s also easy to shop around for the best prices and takes less time than driving to the shops.

Mvc Design Pattern
By: TuVinhSoft .,JSC | 14/11/2008
Model-view-controller (MVC) is an architectural pattern used in software engineering. In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (View) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.

Advantages of Low Cost Contract Programmers in Freelance Programming
By: Joanna Gadel | 12/11/2008
It observed that web industry is getting tougher thus the necessity of freelance contract programmer is required for developing more effective website with flexible features. This article states the fruitful advantages of freelance programmers in contract programming.

A Guide to Cnc Kits
By: Martin Applebaum | 09/11/2008
CNC kits are a way in which to construct your CNC machine. This article will provide some information on these machines.

A Guide to Cnc Tube Bending Machines
By: Martin Applebaum | 08/11/2008
Are you familiar with a CNC tube bending machine? This article will shed some light on the main function and components of this machine.

Ways to Hire Dedicated Php Programmers
By: Jucick | 08/11/2008
It’s not at all easy to hire dedicated PHP programmers unless you know where and how to find them. Whether you need to fix, update or enhance your website you naturally want the job done quick and right.

Top 4 Reasons Why Addressing Web Accessibility is Important
By: Matt Cave | 05/11/2008
There are very high chances that web accessibility is more important to the performance of your web site than you realize. Article takes a look at the top 4 reasons why it would be important to address the issue of web accessibility.

More from Bill Hamilton

Self-taught Php/mysql: a Simple Page Counter Tutorial
By: Bill Hamilton | 04/03/2008 | Programming
This article is a short introduction to PHP and MySQL using the example of a simple page counter. It steps through creating the database in MySQL, connecting to the database from the PHP script, querying the database, displaying the information in a web page, and writing the information back to the database. The article examines the basic syntax of PHP statements and variables, and the fundamental PHP MySQL functions mysql_connect, mysql_select_db, mysql_query, mysql_num_rows, and mysql_result.

Article Categories





Give Feedback

Sign up for our email newsletter

Receive updates, enter your email below