Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 2:34 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 13 posts ]  Go to page 1, 2  Next
Author Question
 Question subject: Tips for PHP scripts
PostPosted: Tue Nov 11, 2008 12:13 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time

With PHP 4.0 comes more than 30 new array-related functions. Some of the more common functions let you determine if something is in a given array or not, count the number of elements in an array, add or delete array elements, and sort array elements.

If you have a large array and all you need to do is find out if a given value exists, you can use in_array() to return true or false. The following will print "Not found in this array" because you're looking for "Albert" in the $namesArray, which does not exist.

Code:
<? $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
$lookingFor = "Albert";

if (
in_array($lookingFor, $namesArray)) {
echo
"You've found it!";
} else {
echo
"Not found in this array!";
}
?>


If you change the value of $lookingFor to "Mary", you will receive the "You've found it!" message, since "Mary" is part of the $namesArray.

If you want to count the number of elements in an array, you can use the handy count() function:
Code:

<? $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
$count = count($namesArray); ?>


The value of $count is 7.

You can add elements to any array, either at the end or the beginning of an existing array. You can also use array_merge() to create a new array consisting of the elements of two or more arrays. When merging, each array is tacked on in the order that it is requested. If your arrays have an internal sort order already in place, you'll have to re-sort your new, merged array.

Let's start with adding elements at the end of an existing array, using

array_push():
Code:

<? /* create the original array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* add to the original array */
array_push($fruitArray, "grape", "pineapple", "tomato");

/* list each element, with its key */
while (list($key,$value) = each($fruitArray)) {
echo
"$key : $value<br>";
}

?>

This will display:
0 : apple
1 : orange
2 : banana
3 : kiwi
4 : pear
5 : grape
6 : pineapple
7 : tomato

The code is remarkably similar if you need to add some elements to the beginning of an array,. The only difference is the function name: array_unshift() instead of array_push().
Code:

<?
/* create the original array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* add to the original array */
array_unshift($fruitArray, "grape", "pineapple", "tomato");

/* list each element, with its key */
while (list($key,$value) = each($fruitArray)) {
echo
"$key : $value<br>";
}

?>

This will display:
0 : grape
1 : pineapple
2 : tomato
3 : apple
4 : orange
5 : banana
6 : kiwi
7 : pear

The array_merge() function smashes two or more arrays together.
Code:

<? /* create the first array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* create the second array */
$vegArray = array("carrot", "green beans", "asparagus", "artichoke", "corn");

/* merge the arrays into one */
$goodfoodArray = array_merge($fruitArray, $vegArray);

/* list each element, with its key */
while (list($key,$value) = each($goodfoodArray)) {
echo
"$key : $value<br>";
}

?>

This will display:
0 : apple
1 : orange
2 : banana
3 : kiwi
4 : pear
5 : carrot
6 : green beans
7 : asparagus
8 : artichoke
9 : corn

Now that you've added and merged arrays, let's run through the functions for deleting elements from arrays. You can delete one element from the end of an array, using array_pop(). If you use array_shift(), you're deleting an element from the beginning of an array. While you are in fact deleting the element from the array, this element is still available to you as a variable, when you pop it or shift it from the existing array.

Try using array_pop() to delete a value from the end of an array:
Code:

<?
/* create an array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* pop something off the end */
$popped = array_pop($fruitArray);

/* list the contents of the new array, as well as the value you popped off */
while (list($key,$value) = each($fruitArray)) {
echo
"$key : $value<br>";
}

echo
"<br>and finally, in $popped: $popped";

?>


This will display:
0 : apple
1 : orange
2 : banana
3 : kiwi

and finally, in $popped: pear

Next, delete an element from the end of an array:

Code:
<?
/* create an array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* shift something from the beginning */
$shifted = array_shift($fruitArray);

/* list the contents of the new array, as well as the value you shifted off */
while (list($key,$value) = each($fruitArray)) {
echo
"$key : $value<br>";
}

echo
"<br>and finally, in $shifted: $shifted";

?>


This will display:
0 : orange
1 : banana
2 : kiwi
3 : pear

and finally, in $shifted: apple

There are several functions that assist you in sorting array elements, but I'll show you just the basic sort so that you understand the process:
Code:

<? /* create the original array */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* sort the array */
sort($fruitArray);

/* reset it so you can display it properly from beginning to end */

/* list each element, with its key */
while (list($key,$value) = each($fruitArray)) {
echo
"$key : $value<br>";
}

?>

This will display:
0 : apple
1 : banana
2 : kiwi
3 : orange
4 : pear

 Article by:   Julie Meloni

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Tips for PHP scripts
PostPosted: Sat Jan 10, 2009 5:45 pm 
Offline
Newbie
User avatar

Joined: Sat Nov 01, 2008 10:28 am
Posts: 20
Has thanked: 0 time
Have thanks: 0 time
provided useful information

_________________
http://www.vikneshblog.blogspot.com


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Tips for PHP scripts
PostPosted: Sun Feb 15, 2009 3:59 pm 
Offline
Newbie
User avatar

Joined: Sun Feb 15, 2009 3:40 am
Posts: 6
Has thanked: 0 time
Have thanks: 0 time
Thanks for the tips, I would like to know the shortest way to code PHP. :D


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Tips for PHP scripts
PostPosted: Sat May 23, 2009 12:39 pm 
Offline
Newbie
User avatar

Joined: Tue Apr 07, 2009 2:09 pm
Posts: 13
Has thanked: 0 time
Have thanks: 0 time
thanks for your wonderful tips :)

_________________
web design chennai


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Tips for PHP scripts
PostPosted: Sun Jul 19, 2009 8:08 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
thank you for added php script code .

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Tips for PHP scripts
PostPosted: Wed Jul 22, 2009 10:31 am 
Offline
Newbie
User avatar

Joined: Wed Apr 15, 2009 7:18 am
Posts: 32
Has thanked: 0 time
Have thanks: 0 time
How do I avoid Yahoo's bulk folder when sending new users a welcome email from my php script?

_________________
MySpace Layouts


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Tips for PHP scripts
PostPosted: Wed Jul 22, 2009 11:30 am 
Offline
Newbie
User avatar

Joined: Wed Jul 22, 2009 11:07 am
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
Thank you for that useful information,I a learner in PHP trying to building some website which would have some database in backend;i.e Mysql,so can you please help me out.




___________________________
http://www.bestategyptholidays.co.uk
http://5dc4dh6kogpnk6vi2by62l2l4v.hop.clickbank.net/


Last edited by arbaba on Sun Aug 23, 2009 11:46 pm, edited 3 times in total.

TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Tips for PHP scripts
PostPosted: Wed Jul 29, 2009 2:49 pm 
Offline
Newbie
User avatar

Joined: Wed Jul 29, 2009 2:44 pm
Posts: 2
Has thanked: 0 time
Have thanks: 0 time
Try this for upload.php:

Code:
$new_dir 'uploads/';
$this_file_name strtolower($_FILES['userfile']['name']);
copy("$userfile"$new_dir $this_file_name); 

Also, when dealing with file uploads & security, you should validate the extension - malicious code could be run on your server via file upload.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Tips for PHP scripts
PostPosted: Wed Jul 29, 2009 11:28 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
well if you have more important tips , please share .

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Tips for PHP scripts
PostPosted: Thu Jul 30, 2009 6:06 am 
Offline
Newbie
User avatar

Joined: Mon Jul 27, 2009 7:14 am
Posts: 13
Has thanked: 0 time
Have thanks: 0 time
Hi,
How do I take information from a session and add it to a database? I want to take the persons username of the person submitting a the form and add it to a database.


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 13 posts ]  Go to page 1, 2  Next
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 Some basic tips for SEO  Google  natasha9211  7
 PHP:How to add digest to the php scripts?  PHP  mstks  0
 SEO TIPS  General Discussion  shephil100  24
 Chuk-Munn Lee Provides the Latest JDK 6 Troubleshooting Tips  General Discussion  msi_333  2

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team