Skip to content

Instantly share code, notes, and snippets.

@vonnenaut
Last active July 6, 2021 11:44
Show Gist options
  • Save vonnenaut/79f37ec216a927e413758a04579da70e to your computer and use it in GitHub Desktop.
Save vonnenaut/79f37ec216a927e413758a04579da70e to your computer and use it in GitHub Desktop.
PHP

PHP

Basics

Installation

  1. Download and extract source code into C:\php

  2. copy C:\php\php.ini-development into a new file called C:\php\php.ini

  3. edit php.ini to include any required extensions, such as:

    extension=curl
    extension=gd
    extension=mbstring
    extension=pdo_mysql
    

    If you want to send emails using PHP’s mail() function, enter the details of an SMTP server in the [mail function] section (your ISP’s server should be suitable):

    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    SMTP = mail.myisp.com
    ; http://php.net/smtp-port
    smtp_port = 25
    
    ; For Win32 only.
    ; http://php.net/sendmail-from
    sendmail_from = [email protected]
  4. Add C:\php to PATH environment variable

    Windows Start --> environment --> Edit system environment variables --> Advanced --> Environment Variables button --> scroll down System variables list --> click Path --> Edit button --> New --> add C:\php

Delineator in HTML

<html>
  <head></head>
  
  <body>
    <?php

    ?>
  </body>
</html>

Setup - XAMPP on Windows 10

https://www.apachefriends.org/index.html

https://www.youtube.com/watch?v=X0_pthMQPMM

XAMPP (Win 10) - start control panel, then open browser to http://localhost/dashboard/ to access GUI for PHP, database, etc.

  • To run .php files in your local browser, copy them into a subfolder in C:\php\htdocs\ and then open them from there using the path localhost/[subfolder]/file.php in your web browser after XAMPP is running the apache server and any other needed resources
  • phpinfo() - set display_errors to on during development but make sure it's set to off in production;

Change setting:

  1. press config button next to apache in XAMPP Control Panel
  2. php.ini
  3. ctrl + f 'display_errors'
  4. save file
  5. restart apache server
  6. go to admin page, phpinfo(), double-check setting is correct

Notes

  • short-open tags are bad form (<? ?>, <?= ?>)
  • ASP-style tags are very bad form (<%= %>)
  • the above reduce portability and require changes to .ini file to work
  • whitespace doesn't matter
  • <?php phpinfo(); ?>
  • php needs to be saved in files ending in .php

Basics

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <?php
    // single-line comment
    # or like this
  
    /* multi-line
       comments
    */
    ?>
    <?php echo "Hello world<br />"; ?>
    <?php 
      echo 2 + 3; // --> 5
      $greeting = "Hello";
      $target = "World";
      $phrase = $greeting . " " . $target;
      echo $phrase;
      echo "$phrase again, another way to concatenate<br />";
    ?> 
  </body>
</html>

Variables

  • start with $, followed by letter or underscore
  • can contain letters, numbers, underscores or dashes
  • no spaces, case-sensitive

Strings

  • concatenate strings with a dot $phrase = $greeting . " " . $target;
  • concatenate variables with string literals using double quotes, optionally, make it very clear visually with braces, i.e., echo "{$phrase} again<br />";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment