Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
PHP for NonProgrammers (DrupalCon SF 2010)
1. Today’s Tools
Write code: http://www.codepad.com
Select PHP as the language!
Ask questions:
IRC: irc.freenode.net #php4np
Twitter: php4np
Robert Ristroph is here for you!
2. PHP For NonProgrammers
Programmer: One who writes computer programs.
NonProgrammer: One who will write PHP code today
while learning Diana’s Five Rules of Programming
and earning geek points.
DrupalCon San Francisco 2010
3. You Win!
Score more than 5 points during today’s
presentation and win the Grand Prize Geek Award*
(GPGA).
* Four Kitchens’ stickers and the right to change your Facebook language to
English(Pirate).
4. <?php
/**
* About Me
*/
// also comments
$name = “Diana Montalion Dupuis”;
$fourKitchensWebChef = TRUE;
$numPets = 3;
$currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf
Hall”);
if ($notWorking == TRUE) {
echo “I’m reading a book or watching football.”;
} else {
echo “I’m writing PHP code.”;
}
?>
Write your own version!
5. <?php
/** comments
* About Me
*/
// also comments
$name = “Diana Montalion Dupuis”;
$fourKitchensWebChef = TRUE;
$numPets = 3;
$currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf
Hall”);
if ($notWorking == TRUE) {
echo “I’m reading a book or watching football.”;
} else {
echo “I’m writing PHP code.”;
}
?>
Write your own version!
6. <?php
/** comments
* About Me
*/
// also comments
$name = “Diana Montalion Dupuis”;
$fourKitchensWebChef = TRUE; variables
$numPets = 3;
$currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf
Hall”);
if ($notWorking == TRUE) {
echo “I’m reading a book or watching football.”;
} else {
echo “I’m writing PHP code.”;
}
?>
Write your own version!
7. <?php
/** comments
* About Me
*/
// also comments
$name = “Diana Montalion Dupuis”;
$fourKitchensWebChef = TRUE; variables
$numPets = 3;
$currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf
Hall”);
if ($notWorking == TRUE) {
array
echo “I’m reading a book or watching football.”;
} else {
echo “I’m writing PHP code.”;
}
?>
Write your own version!
8. <?php
/** comments
* About Me
*/
// also comments
$name = “Diana Montalion Dupuis”;
$fourKitchensWebChef = TRUE; variables
$numPets = 3;
$currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf
Hall”);
if ($notWorking == TRUE) {
array
echo “I’m reading a book or watching football.”;
} else {
echo “I’m writing PHP code.”; conditional
}
?>
Write your own version!
10. What does PHP mean?
Who cares?!?
http://en.wikipedia.org/wiki/PHP
11. What does PHP mean?
Who cares?!?
The question and answer that matter most are the
http://en.wikipedia.org/wiki/PHP
12. What does PHP mean?
Who cares?!?
The question and answer that matter most are the
PHP: Hypertext Preprocessor is a widely used, general-purpose 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. As a general-purpose programming language, PHP code is
processed by an interpreter application in command line mode performing desired operating system operations and producing program
output on its standard output channel. It may also function as a graphical application. PHP is available as a processor for most modern
web servers and as a standalone interpreter on most operating systems and computing platforms.
http://en.wikipedia.org/wiki/PHP
14. Diana’s Rule #1
Self educate constantly.
Your sixth-grade English teacher was right - if you
don’t know, look it up. Find places to get help.
Ignore whatever (or whomever) isn’t helpful.
Programming is asking the right questions and coming up with
possible answers.
26. Write Code!
1. Use Codepad or open Notepad (or any other
simple text program).
I recommend TextMate and Coda for Mac, EditPlus and Aptana for PCs
27. Write Code!
1. Use Codepad or open Notepad (or any other
simple text program).
2. Not Microsoft Word, never ever ever use Word. A
piece of paper and a pen is fine too.
I recommend TextMate and Coda for Mac, EditPlus and Aptana for PCs
28. Write Code!
1. Use Codepad or open Notepad (or any other
simple text program).
2. Not Microsoft Word, never ever ever use Word. A
piece of paper and a pen is fine too.
2. Write this in the top left-hand corner:
I recommend TextMate and Coda for Mac, EditPlus and Aptana for PCs
35. Diana’s Rule #3
You will suffer daily from the Missing Semicolon
Syndrome (MSS).
Forgetting the semicolon is the most common syntax
error. PHP errors will break your site. Don’t panic!
Just fix them.
“Stupid” mistakes are 85% of mistakes you’ll make.
38. Four PHP Concepts
• Variables: containers for your stuff
• Arrays: a variable that is a list, or a list of lists
39. Four PHP Concepts
• Variables: containers for your stuff
• Arrays: a variable that is a list, or a list of lists
• Conditionals: the brains of your operation
40. Four PHP Concepts
• Variables: containers for your stuff
• Arrays: a variable that is a list, or a list of lists
• Conditionals: the brains of your operation
• Functions: little blocks of “do stuff”
62. Foreach is your friend
$myPets = array(“Maya”, “Cloe”, “August”);
foreach ($myPets as $pet) {
echo $pet;
echo “<br />”;
}
// $pet is a variable you create
63. Maya
Cloe
August
Foreach is your friend
$myPets = array(“Maya”, “Cloe”, “August”);
foreach ($myPets as $pet) {
echo $pet;
echo “<br />”;
}
// $pet is a variable you create
64. Keys and Values
$actor = array(
‘name’ => “John Cleese”,
‘nickname’ => “Bruce”,
‘job’ => “Minister of Silly Walks”,
);
echo $actor[‘nickname’];
this is very common in Drupal coding
65. Bruce
Keys and Values
$actor = array(
‘name’ => “John Cleese”,
‘nickname’ => “Bruce”,
‘job’ => “Minister of Silly Walks”,
);
echo $actor[‘nickname’];
this is very common in Drupal coding
66. <?php
/**
* About Me
*/
$name = “Your Name Here”;
$yourList = array(“Thing1”, “Thing2”, “Thing3”);
?>
Write your own version!
67. <?php
/**
* About Me add some
*/ variables
$name = “Your Name Here”;
$yourList = array(“Thing1”, “Thing2”, “Thing3”);
?>
Write your own version!
68. <?php
/**
* About Me add some
*/ variables
$name = “Your Name Here”;
$yourList = array(“Thing1”, “Thing2”, “Thing3”);
add an array
?>
Write your own version!
82. Let’s Talk Boolean
• My name is Diana
• My name is Diana && I own a motorcycle
• My name Diana || I am younger than 29
83. Let’s Talk Boolean
• My name is Diana
• My name is Diana && I own a motorcycle
• My name Diana || I am younger than 29
• (My name is Diana || I am younger than 29) && I
own a motorcycle
86. Conditionals are Control
if ($captain == “Picard”) {
echo “Make it so, Number One.”;
} elseif ($captain == “Kirk”) {
echo “Two to beam up.”;
} else {
echo “Please state the nature of your medical
emergency.”;
}
87. <?php
/**
* Boolean Example
*/
$captain = “Janeway”;
if ($captain == “Picard”) {
echo “Make it so, Number One.”;
} elseif ($captain == “Kirk”) {
echo “Two to beam up.”;
} else {
echo “Please state the nature of your medical emergency.”;
?>
There is an MSS issue in this code, can you find it? Hint: }
88. <?php Please state the nature of your medical
emergency.
/**
* Boolean Example
*/
$captain = “Janeway”;
if ($captain == “Picard”) {
echo “Make it so, Number One.”;
} elseif ($captain == “Kirk”) {
echo “Two to beam up.”;
} else {
echo “Please state the nature of your medical emergency.”;
?>
There is an MSS issue in this code, can you find it? Hint: }
89. <?php Please state the nature of your medical
emergency.
/**
* Boolean Example
*/
$captain = “Janeway”; MSS ALERT!
if ($captain == “Picard”) {
echo “Make it so, Number One.”;
} elseif ($captain == “Kirk”) {
echo “Two to beam up.”;
} else {
echo “Please state the nature of your medical emergency.”;
?>
There is an MSS issue in this code, can you find it? Hint: }
90. <?php
/**
* About Me
*/
$name = “Your Name Here”;
$yourList = array(“Thing1”, “Thing2”, “Thing3”);
if (variable == something && variable == something) {
print “A clever sentence”;
}
?>
Write your own version!
91. <?php
/**
* About Me
*/
variables
$name = “Your Name Here”;
$yourList = array(“Thing1”, “Thing2”, “Thing3”);
if (variable == something && variable == something) {
print “A clever sentence”;
}
?>
Write your own version!
92. <?php
/**
* About Me
*/
variables
$name = “Your Name Here”;
$yourList = array(“Thing1”, “Thing2”, “Thing3”);
if (variable == something && variable == something) {
print “A clever sentence”;
}
add a conditional
?>
Write your own version!
93. A bonus point if you’ve read the book && seen the movie
94. Hello. My name is Inigo Montoya.
You killed my father. Prepare to
die.
A bonus point if you’ve read the book && seen the movie
95. Functions
Functions encapsulate code that does a specific task.
The function can be called whenever the task needs
to be performed.
Tutorial: http://www.tizag.com/phpT/phpfunctions.php
96. // call the function to count the fingers
$killHim = isSixFingeredMan($numFingers);
if ($killHim) {
echo “Hello. My name is Inigo Montoya. You killed my
father. Prepare to die.”;
}
/**
* Returns true if number of fingers is 6
*/
function isSixFingeredMan($numFingers) {
if ($numFingers == 6) {
return TRUE;
} else {
return FALSE;
}
}
97. // call the function to count the fingers
$killHim = isSixFingeredMan($numFingers);
call
if ($killHim) {
echo “Hello. My name is Inigo Montoya. You killed my
father. Prepare to die.”;
}
/**
* Returns true if number of fingers is 6
*/
function isSixFingeredMan($numFingers) {
if ($numFingers == 6) {
return TRUE;
} else {
return FALSE;
}
}
98. // call the function to count the fingers
$killHim = isSixFingeredMan($numFingers);
call
if ($killHim) {
echo “Hello. My name is Inigo Montoya. You killed my
father. Prepare to die.”;
}
/**
* Returns true if number of fingers is 6
*/
function isSixFingeredMan($numFingers) {
if ($numFingers == 6) {
return TRUE;
} else { function
return FALSE;
}
}
99. // call the function to count the fingers
$killHim = isSixFingeredMan($numFingers);
call
if ($killHim) {
echo “Hello. My name is Inigo Montoya. You killed my
father. Prepare to die.”;
} continue on
/**
* Returns true if number of fingers is 6
*/
function isSixFingeredMan($numFingers) {
if ($numFingers == 6) {
return TRUE;
} else { function
return FALSE;
}
}
103. Function Facts
• include the word “function”
• should be aptly named
• can accept one or more parameters
104. Function Facts
• include the word “function”
• should be aptly named
• can accept one or more parameters
• can return something
105. Function Facts
• include the word “function”
• should be aptly named
• can accept one or more parameters
• can return something
• should be used for repeated tasks and to organize
logic
106. Function Facts
• include the word “function”
• should be aptly named
• can accept one or more parameters
• can return something
• should be used for repeated tasks and to organize
logic
• can be public or private
107. <?php
/**
* Function Fun
*/
$yourVariable = “Something”;
$returnedVariable = NULL;
$returnedVariable = yourFunction($yourVariable);
echo $returnedVariable;
public function yourFunction($yourVariable) {
$greeting = “Here’s a sentence containing “. $yourVariable;
return $greeting;
}
?>
Write your own version!
108. <?php
/**
* Function Fun
*/
$yourVariable = “Something”; define a variable
$returnedVariable = NULL;
$returnedVariable = yourFunction($yourVariable);
echo $returnedVariable;
public function yourFunction($yourVariable) {
$greeting = “Here’s a sentence containing “. $yourVariable;
return $greeting;
}
?>
Write your own version!
109. <?php
/**
* Function Fun
*/
$yourVariable = “Something”; define a variable
$returnedVariable = NULL;
$returnedVariable = yourFunction($yourVariable);
echo $returnedVariable;
call your function
public function yourFunction($yourVariable) {
$greeting = “Here’s a sentence containing “. $yourVariable;
return $greeting;
}
?>
Write your own version!
110. <?php
/**
* Function Fun
*/
$yourVariable = “Something”; define a variable
$returnedVariable = NULL;
$returnedVariable = yourFunction($yourVariable);
echo $returnedVariable;
call your function
public function yourFunction($yourVariable) {
$greeting = “Here’s a sentence containing “. $yourVariable;
return $greeting;
}
add a function
?>
Write your own version!
111. <?php
/**
* Function Fun
*/
$yourVariable = “Something”; define a variable
$returnedVariable = NULL;
$returnedVariable = yourFunction($yourVariable);
echo $returnedVariable;
public function yourFunction($yourVariable) {
$greeting = “Here’s a sentence containing “. $yourVariable;
return $greeting;
}
add a function
?>
Write your own version!
112. <?php
/**
* Function Fun
*/
$yourVariable = “Something”; define a variable
$returnedVariable = NULL;
$returnedVariable = yourFunction($yourVariable);
echo $returnedVariable;
continue on
public function yourFunction($yourVariable) {
$greeting = “Here’s a sentence containing “. $yourVariable;
return $greeting;
}
add a function
?>
Write your own version!
113. <?php Here’s a sentence containing Something.
/**
* Function Fun
*/
$yourVariable = “Something”; define a variable
$returnedVariable = NULL;
$returnedVariable = yourFunction($yourVariable);
echo $returnedVariable;
continue on
public function yourFunction($yourVariable) {
$greeting = “Here’s a sentence containing “. $yourVariable;
return $greeting;
}
add a function
?>
Write your own version!
114. What does he do for a living?
Score one point for each title you can name
118. Diana’s Rule #5
Follow best practices.
Like the rules of the road, best practices prevent
(coding) accidents. They also make you a likable
person.
http://drupal.org/node/287350