Php string function for sem 6 gtu, Cheat Sheet of Compilers

Good work for php subject of sem 6

Typology: Cheat Sheet

2021/2022

Uploaded on 01/17/2022

shreekar-patel
shreekar-patel 🇮🇳

1 document

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PHP Functions / Modularity
Dr. Charles Severance
www.wa4e.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Php string function for sem 6 gtu and more Cheat Sheet Compilers in PDF only on Docsity!

PHP Functions / Modularity

Dr. Charles Severance

www.wa4e.com

Why Functions?

  • PHP has lots of built-in functions that we use all

the time.

  • We write our own functions when our code

reaches a certain level of complexity.

PHP Documentation - Google

PHP Documentation - Google

Defining Your Own Functions We use the function keyword to define a function, we name the function and take optional argument variables. The body of the function is in a block of code { } function greet() { print "Hello\n"; } greet(); greet(); Hello Hello

Choosing Function Names

  • Much like variable names - but do not start with a dollar sign
  • Start with a letter or underscore - consist of letters, numbers, and underscores ( _ )
  • Avoid built-in function names
  • Case does not matter^ – but please do not take advantage of this

Arguments Functions can choose to accept optional arguments. Within the function definition the variable names are effectively “aliases” to the values passed in when the function is called. function howdy($lang) { if ( $lang == 'es' ) return "Hola"; if ( $lang == 'fr' ) return "Bonjour"; return "Hello"; } print howdy('es'). " Glenn\n"; print howdy('fr'). " Sally\n"; Hola Glenn Bonjour Sally

Optional Arguments Arguments can have defaults, and so can be omitted. function howdy($lang='es') { if ( $lang == 'es' ) return "Hola"; if ( $lang == 'fr' ) return "Bonjour"; return "Hello"; } print howdy(). " Glenn\n"; print howdy('fr'). " Sally\n"; Hola Glenn Bonjour Sally

Call By Reference Sometimes we want a function to change one of its arguments, so we indicate that an argument is “by reference” using ( & ). function triple(&$realthing) { $realthing = $realthing * 3; } $val = 10; triple($val); echo "Triple = $val\n"; Triple = 30

http://php.net/manual/en/function.sort.php

Variable Scope

  • In general, variable names used inside of function code do not mix with the variables outside of the function to avoid unexpected side effects if two programmers use the same variable name in different parts of the code.
  • We call this “name spacing” the variables.^ The function variables are in one “name space” whilst the main variables are in another “name space”. http://php.net/manual/en/ language.variables.scope.php

Normal Scope (isolated) function tryzap() { $val = 100; } $val = 10; tryzap(); echo "TryZap = $val\n"; TryZap = 10 http://php.net/manual/en/language.variables.superglobals. Except for $_GET

Global Variables – Use Rarely

  • Passing variable in as parameter
  • Passing value back as return value
  • Passing variable in by reference
  • If you use global variables, use long names with nice unique prefixes global $LastOAuthBodyBaseString; global $LAST_OAUTH_BODY_BASE_STRING;

Coping with Missing Bits Sometimes, depending on the version or configuration of a particular PHP instance, some functions may be missing. We can check that... if (function_exists("array_combine")){ echo "Function exists"; } else { echo "Function does not exist"; } This allows for evolution.