18
Beginning PHP Session #2 November 17, 2010 Josh Butts

Geek Austin PHP Class - Session 2

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Geek Austin PHP Class - Session 2

Beginning PHPSession #2

November 17, 2010

Josh Butts

Page 2: Geek Austin PHP Class - Session 2

Agenda for Today

•Go over Homework #1

•Include & Require

•Errors & Warnings

•String Functions

•Writing Your Own Functions

Page 3: Geek Austin PHP Class - Session 2

Corrections

•Use “echo $foo”, not “echo($foo)”

•I think we forgot to talk about code comments, so lets do that now

Page 4: Geek Austin PHP Class - Session 2

Comments

Page 5: Geek Austin PHP Class - Session 2

A note on constructs vs.

functions•A language construct is a core part of the language, like “true”, “include” and “isset”

•Some things in PHP behave like functions, but are actually language constructs

Page 6: Geek Austin PHP Class - Session 2

String Functions

•You’ll use these all the time

•The internet deals in strings

•Much like array functions, there are quite a few

Page 7: Geek Austin PHP Class - Session 2

strlen()

•How long is a string, in characters?

•As long as this isn’t UTF-8-ish

Page 8: Geek Austin PHP Class - Session 2

substr()

•Get a smaller part of the whole string

•Operates by “index”

Page 9: Geek Austin PHP Class - Session 2

str_replace()

•search and replace strings within strings

•use arrays for extra power

Page 10: Geek Austin PHP Class - Session 2

strpos()

• Finds out the “index” location of a string within a string

• Commonly used to find if a string exists within another

Page 11: Geek Austin PHP Class - Session 2

Changing Case

Page 12: Geek Austin PHP Class - Session 2

explode()

•Split up a string on certain characters and give back an array of the pieces

Page 13: Geek Austin PHP Class - Session 2

implode()

•Build a string from an array

Page 14: Geek Austin PHP Class - Session 2

Functions

•Functions are ways to encapsulate & organize your code

•Hundreds of built-in functions

•Calculate values, perform repetitive tasks, output data

Page 15: Geek Austin PHP Class - Session 2

Write Your Own Functions

Page 16: Geek Austin PHP Class - Session 2

Include

•Load another PHP file into the current contexts

•Executes that code as if it were in the current file

•Gives a warning if the file is not found

Page 17: Geek Austin PHP Class - Session 2

Include_Once

•The same as include, except keeps track of if you’re already included the file and only includes it once

•Use but don’t abuse, there is an implicit speed penalty for doing this

Page 18: Geek Austin PHP Class - Session 2

Require

•Functions the same way as include except gives a fatal error if the file is not found

•Tends to be used for system components vs. small output snippets

•Require_once == include_once