3w PHP Your Source for PHP Source Code


Checking if string exist in array values

In  this sample, we create array with names for a group.
Then we check to see if a specific name is in the group...

Here is the sample.

// Array of names in group
$array = array('Sam' , 'Louis' , 'Samentha' , 'Joe');
if (in_array('Joe',$array))
	echo 'Joe is in group...';

Thank you and please, do not hesitate to comment.

Filed under: General, PHP Basics 2 Comments

Ask a programmer php question

Here you can use the comments to ask the programmer any question you wish regarding PHP, MySql, HTML, CSS, etc...
he programmer will then answer your question in another post, and reply to your comment here, so you will get an email notification.

Enjoy learning.
Manny E.
3wPHP Staff.

Filed under: General, PHP Basics 1 Comment

Function to compare 2 string values

This function will compare 2 values,
make sure they are strings,
and make sure they are not empty

here is the code.

function str_compare($str1 , $str2)
	// we are converting the values to strings...
	// comment out the following 2 lines 
	// if you wish to check other value types.
	$str1 = (string)$str1;
	$str2 = (string)$str2;
	/// we are checking to see that both values are not empty.
	/// if one of the strings is empty, we return false.
	if (empty($str1) || empty($str2))
		return false;
	// the following line is the same like doing
	if($str1 === $str2)
		$same = true;
		$same = false;
	$same = (($str1 === $str2)? true : false);
	return $same;

enjoy learning, and please,
feel free to comment, or ask anything you want to know


Welcome to 3wPHP.com!

Welcome to 3wPHP.com. This is our first post. We will be posting some code soon.

echo 'Welcome' ;

Thank you
Manny E.
3wphp staff.

Filed under: General 1 Comment

How to convert array to object

When you want to convert array to object, you will usually use a named(associative) array rather then numbered array...


$users = array();
$users['mark_long'] = array(
					'id' => 1 ,
					'firstname' => 'Mark',
					'lastname' => 'Long',
					'phone' => '718-555-5555'
$users['john_smith'] = array(
					'id' => 2 ,
					'firstname' => 'John',
					'lastname' => 'Smith',
					'phone' => '718-555-5555'
$users['sam_jonson'] = array(
					'id' => 3 ,
					'firstname' => 'Sam',
					'lastname' => 'Jonson',
					'phone' => '718-555-5555'
foreach ($users as $username => $user_data)
	$user_data = (object)$user_data;
	//// now we can access he data using the object...
	//// so instead of using $user_data['firstname'], 
	//// we will be using $user_data->firstname ...
	echo 'User ID: '. $user_data->id .'<br />' ;
	echo 'Full Name: '.$user_data->firstname .' ' . $user_data->lastname .'<br />';
	echo 'Phone Number: '.$user_data->phone .'<br />' ;

Well, here we just did something very simple...
Feel free to comment on this, or ask any questions you might have.
Thank you.

Filed under: PHP Basics No Comments