Web Design Forum: TUTORIAL: PHP tips + tricks - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: PHP tips + tricks Rate Topic: -----

#1 User is offline   gold killer 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 15
  • Joined: 02-June 07
  • Reputation: 0
  • Gender:Male
  • Location:Chelmsford, Essex
  • Experience:Advanced
  • Area of Expertise:Entrepreneur

Posted 03 June 2007 - 09:38 PM

Recently I have been reading posts where people have been saying one thing is better or worse than another so i thought id do this tutorial to clarify these things and also teach you some little tricks in the process...

Echo, Print, die and Return
These are all similar in the fact that they display text if entered into them. That can be done like so:

echo 'text';
print 'text';
return 'text';
die 'text';

So what do these all do?
Echo is very similar to print with the way it acts but the only difference is that it returns text and a value of NULL / FALSE.
eg1
<?php
	function Test()
	{
	   echo 'This is a test';
	}

	$tryTest = Test(); //Define a variable to a function
	if( $tryTest )//If there is something in the variable
	{
	   echo '(Y)';//Show a done message
	}
	else//If not
	{
	   echo '(N)';//Show a not done message
	}
?>


For this example you would be shown (N) as echo has displayed the text but hasnt told the script that is has done so, therefore thinking nothing has been shown.

Print on the other hand does return TRUE so if we replace all our echo's with print we would get (Y).
eg2
<?php
	function Test()
	{
	   print 'This is a test';
	}

	$tryTest = Test(); //Define a variable to a function
	if( $tryTest )//If there is something in the variable
	{
	   print '(Y)';//Show a done message
	}
	else//If not
	{
	   print '(N)';//Show a not done message
	}
?>


Now we have gone through the difference between print and echo we are left with return and die...

Return is used mainly in functions when you want to send text but not display it yet. Return also means that the script stops because it is used when you have gotten to the end of an algarithm...
eg3
<?php
	function Test()//A new function called test
	{
		$q = b;

		if( 'y' == 'n' )
	   {
		  return 'Done';
	   }
	   elseif( 't' != $q )
	   {
		  return 'Done';
	   }
	   else
	   {
		  return FALSE;
	   }

	}//End function

	if( Test() )
	{
	   print 'This won\'t be shown as the function Test has returned a negative value ';
	}
?>


Now you maybe wondering what return FALSE is doing...

With return you can return a value without any text involved, so return FALSE; ends the script but it doesnt return a value so if assigned to a variable it wouldnt work; as shown in the above example.

Die must be the most dramatic of all php predefined functions all it basically does is end the script and return a negative value. It is used like so:
eg4
die('Test');

or just
die 'Test';


Quotation marks

Now we can move onto the difference between quotation marks and dashes.

When using php with MySQL you may see 3 different marks like so:

`
'
"

The first one ` is used in MySQL to wrap arround table names and field names (it is found on the top left of your keyboard). It is used like so:
SELECT `id`, `name`, `delete` FROM `tablename`;


The above statement would work but in MySQL this would not work
SELECT 'id', 'name', 'delete' FROM 'tablename';


The above would not work as it is telling MySQL that the values are strings relating to field such as
`name` = 'frank';


In PHP the single quotation mark ' is used to display text and only text.
eg5
<?php

	print 'This is text';				//This would simply show:
											//This is text

	$fruit = 'apples'

	print 'I love my $fruit';		//This would show:
										 //I love my $fruit
   
				 //The variable is treated as text so is not replaced by the string assigned to it

				//To make the variable work you can do 2 things...

	print 'I love my ' . $fruit;	//This would show:
										//I love my apples

				//The above print function ends the text string by adding the second single quotation
			
				//But the print function is not finished untill you add; so you can add a . and the variable will be added after the string infront of it.

?>


Another way to display the variable is to use double quotations, this treats everything as a variable so takes abit longer to render the text (it still works but just abit slower). You can use the double quotations like so...

eg6
<?php
   
	$fruit = 'oranges';

	print "I love to eat $fruit all the time";	  //This will display
											  //I love to eat oranges all the time
   
	//A bad thing with this is that $fruit won't be highlighted in the variable colour so it will be harder on the eye to spot in long lines of code so you can do

	print "I love to eat ".$fruit." all the time";	  //This will display
											  //I love to eat oranges all the time
?>


You maybe wondering what happens when you need to use that quotation mark inside itself, this is done like so:
eg7
print "gold killer said: \"Hi\" ";	//This would display -  gold killer said: "Hi"

print 'gold killer said: \"Hi\" ';	//This would display -  gold killer said: \"Hi\" - This is because the double quotations can be used normaly inside single quotations and visa versa, like so:

print "gold killer said: 'Hi' ";	  //This would display - gold killer said: 'Hi'

print 'gold killer said: 'Hi';		 //This would not work as the single quotations need a \ infront of them like so:

print 'gold killer said: \'Hi\'\;	  //This would work and would display - gold killer said: 'Hi'


Now you could be thinking thats going to be a pain when displaying big chunks of xhtml, PHP 5 has a simple sollution like below:

eg8
<?php
   
	echo<<<END
	<form method="post" action="test.php">
		Forename: <input type="text" name="fname" /><br />
		Surname: <input type='text' name='sname' /><br />
	   <input type="submit" value='GO!' />
	</form>
	END;
?>


Thats the simple and easiest way of displaying big chunks of html in PHP 5. In PHP 4 you can do that like so:
eg9
<?php
	//PHP here
	?>

	<form method="post" action="test.php">
		Forename: <input type="text" name="fname" /><br />
		Surname: <input type='text' name='sname' /><br />
	   <input type="submit" value='GO!' />
	</form>

	<?php
	//PHP here
?>


But one problem with that is that you can't display PHP variables...
You may think about starting it all up again and doing <?php echo $var; ?>

You maybe thinking, surley php would not be this dumb... its not! Below is the quick echo:
eg10
<?php
	$var = 'GO!';//Define our variable
	?>

	<form method="post" action="test.php">
		Forename: <input type="text" name="fname" /><br />
		Surname: <input type='text' name='sname' /><br />
	   <input type="submit" value="<?=$var?>" />
	</form>

	<?php
?>


So to do a quick echo all you need is
<?=$variableName?>

or
<?=$variableName;?>


\t \s \n
These are used as breaks in the code as php does not automatically add line breaks in your code when using echo or print so to do this you can use
\n

This means when you view source there will be a new line started there!

\t Is a tab value so when added the source gets tabbed accross

\s this is the same as &nbsp; in html!

Below is all of these brought together:
eg11
<?php
	print '
		\t<table>\n
			\t \t<tr>\n
				\t \t \t<td>\s</td>\n
			\t \t</tr>\n
		\t</table>\n
';
?>


This would display:
	<table>
		<tr>
			<td> </td>
		</tr>
	</table>



Thanks for reading, if you need any help just post
0

#2 User is offline   Ben 

  • Web Designer Forum Admin
  • View gallery
  • Group: Root Admin
  • Posts: 2,840
  • Joined: 24-August 06
  • Reputation: 103
  • Gender:Male
  • Location:Essex, UK
  • Experience:Intermediate
  • Area of Expertise:Web Designer

  Posted 04 June 2007 - 12:32 PM

How handy, that's a nice little cheat sheet.

Thanks for posting :)
0

#3 User is offline   Cromo 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 422
  • Joined: 08-April 07
  • Reputation: 0
  • Gender:Male
  • Location:Leeds, UK
  • Experience:Beginner
  • Area of Expertise:Designer/Coder

Posted 04 June 2007 - 12:33 PM

Wat exactly does this do?? sorry i dont really how much knowledge of php.
0

#4 User is offline   olivier 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 279
  • Joined: 22-February 07
  • Reputation: 1
  • Location:Colchester - UK
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 04 June 2007 - 12:40 PM

It makes tea for you if you ask it nicely ;)
0

#5 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 04 June 2007 - 12:42 PM

damm, I don't like tee, I only drink coffee :p

Wildo
0

#6 User is offline   Cromo 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 422
  • Joined: 08-April 07
  • Reputation: 0
  • Gender:Male
  • Location:Leeds, UK
  • Experience:Beginner
  • Area of Expertise:Designer/Coder

Posted 04 June 2007 - 12:44 PM

nice ill use this! ;)
0

#7 User is offline   olivier 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 279
  • Joined: 22-February 07
  • Reputation: 1
  • Location:Colchester - UK
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 04 June 2007 - 12:44 PM

you can get coffee too!
Just do a
SELECT from cupboard WHERE name_on_box = coffee


;)
0

#8 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 04 June 2007 - 12:47 PM

"UPDATE `bank_account' SET `money` = '999999999'"


will that work to?!

Wildo
0

#9 User is offline   Cromo 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 422
  • Joined: 08-April 07
  • Reputation: 0
  • Gender:Male
  • Location:Leeds, UK
  • Experience:Beginner
  • Area of Expertise:Designer/Coder

Posted 04 June 2007 - 01:12 PM

THAT SOUNDS GD!!!
0

#10 User is offline   BenG 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 766
  • Joined: 20-March 07
  • Reputation: 0
  • Location:Bradford, West Yorkshire
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 04 June 2007 - 04:07 PM

View Postolivier, on Jun 4 2007, 01:44 PM, said:

you can get coffee too!
Just do a
SELECT from cupboard WHERE name_on_box = [b]Nescafe Cappacino Sachets[/b]


;)


thats better. :clapping:

Seriously nice tut GoldKiller, you rock, now I know when and when not use them instead of just using echo 24/7 :lol:
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users