Basics of PHP

Understanding the Basics

Of PHP

In this topic, we are going to learn how to program computers to save different type of values, We will learn about the following:

  1. PHP syntax tips
  2. Basics of printing 
  3. Variables
  4. data types

Now that we know what PHP is, let’s start learning the basics of PHP so we can be one step closer to building the drones that would locate crew on Galaxy flight X. 

In this topic we will learn how to build a print system for Agent Gail.

Basic coding

Printing and Syntax in PHP

In PHP, the statements echo or print are used to print outputs on the screen. The only difference between echo and print is echo has no return value while print has a return value of 1. 

A return value is the value that is returned when the print statement has finished printing an output.  In our course, we use echo statements to print out any output on the screen.

Think of it like you’re asking PHP to display or “print” something for the user to see when using the echo and print statements.

 In the image below, the code highlighted is a PHP echo statement.

Writing Comments

In PHP

Sometimes programmers like to add comments to their programs to remind themselves of what they did on particular lines of code or to let everyone who would read their program to understand better.

A comment in PHP code is a line that is not executed as a part of the program – in other words, PHP ignores your comments. So why do we need comments?

Writing comments is good coding practice. Programmers use comments to help other programmers understand the function of certain blocks of code. They also write comments for themselves to refer back to after a long time.

Let’s watch this video to learn about the best comment practices which are further discussed in the video below.

Comments

How to comment in PHP

There are different ways in which comments in php are created.  

(Click on the blue arrow for details on each way)

Take a closer look at the image below to understand how we create comments. When creating single line comments we use the # symbol before the text, as seen in the yellow text example below

#this is a PHP comment

Another single line comment can be created using the symbols // before text, as seen in purple below

//this is a PHP comment

  • Multiple line comments are created using  the symbols /* */, the comments in between the symbols’ as shown in green

/*this is a PHP comment*/

Did you know?

Click here to find out...

PHP is not case sensitive. That means  echo and ECHO are the same, they are not different.

Let’s play around with the echo statement and php comments in the PHP compiler below. You’ll see our php code created around variables.

Your activity is to :

  1. Change the first echo statement to print out “Ready to start the mission,Agent Gail”
  2. Change the second echo statement to print out “Ready to start to looking for the missing spaceship,Agent Gail”
  3. Add two single line php comments to explain what you are doing with the echo statements, Use both # and // to add the comments. For instance you could have a comment saying #Find the lost spaceship. But come up with your own. Play around.

Click on the “fork me” below to edit the PHP code directly and run it.

Now that we know a bit more about PHP let’s create our first program! You will be completing this task in the online PHP compiler below.

Follow the steps to create a program that displays the text “Hello Universe”. 

Here are some steps to guide you:

  1. Let’s print out the text “Hello Universe”.
  2. Add in some comments to help the reader understand like “//here is where to add your print command
  3. Use the echo statement “your text here”; to display your text
  4. Run the program by pressing “Execute or Run”.

We can also use the statement print “your text”; to display your sentence. Why not try both?

Click on the “fork me” below to edit the PHP code directly.

Great job helping Agent Gail print out a message on her drones. 

Next up – let’s learn how to use variables to store more complex information on our drones. 

Next: Let's learn how to use variables

Whitespace – basically empty space (like spaces, tabs, or enter button) that makes the code look neat and organized, but the computer doesn’t care about it.

 A User – is any person that interacts (by inputs) with a program without having to write the code directly. For example, you are the user to the code that makes your browser/website run. When you clicked on this pop-up, that was the input that made this explanation come up when the code detected it.

To book your spot – click here

All learners who are aspiring web developers will have an opportunity to build a website for a live NGO or charity client as part of their community service hours. This project will be run jointly with Community Hours – so all your time spent counts towards your LO credits. This event is suitable for learners, parents and their teachers.

TechWays will be providing the WordPress course and web dev resources for free to any learner wanting to participate. 

Besides the amazing community service you’ll be doing for a charity in need – you’ll also be building your portfolio of web dev skills. Who knows – web dev could become a side hustle for extra income?

Book your spot HERE

Indentation – In the written form of many languages, an indentation or indent is an empty space at the beginning of a line to signal the start of a new paragraph.

Text editor – is the part of the IDE where you write the code. Most text editors highlight words with different properties like functions to help you distinguish them from one another. 

Homogeneous – of the same kind; alike throughout.

Heterogeneous – diverse in character or content; containing different things

Prompt – to  cause or bring about; to make something happen. For example making someone to say or write something.

Troubleshooting is a form of problem solving, often applied to repair failed products or processes on a machine or a system. It is a logical, systematic search for the source of a problem in order to solve it, and make the product or process operational again.

String Built-in Functions/Methods

There are a lot of strings functions/methods in Python. Find full list in course manual. Here’s are some that you find useful in this course:

Functions

  • len(varName) – Returns the length of a list, string
  • join(varName) – Converts the elements of an iterable into a string.

Methods 

  • varName.capitalize() – Converts the first character to upper case.
  • varName.center(length) – Returns a centered string
  • varName.count(“?”) – Returns the number of times a specified value (?) occurs in a string (varName).
  • varName.endswith(“?”) – Returns true if the string ends with the specified value.
  • varName.find(“?”) – Searches the string for a specified value and returns the position of where it was found.
  • varName.format(placeholder = value) – Formats specified values in a string.
  • varName.index(“?”) – Searches the string for a specified value and returns the position of where it was found.
  • varName.isalnum() – Returns True if all characters in the string are alphanumeric.
  • varName.isalpha() – Returns True if all characters in the string are in the alphabet.
  • varName.isascii() – Returns True if all characters in the string are ascii characters.
  • varName.isdecimal() – Returns True if all characters in the string are decimals.
  • varName.isdigit() – Returns True if all characters in the string are digits.
  • varName.isidentifier() – Returns True if the string is an identifier.
  • varName.islower() – Returns True if all characters in the string are lower case.
  • varName.isupper() – Returns True if all characters in the string are upper case.
  • varName.isnumeric() – Returns True if all characters in the string are numeric.
  • varName.isprintable() – Returns True if all characters in the string are printable.
  • varName.isspace() – Returns True if all characters in the string are whitespaces.
  • varName.istitle() – Returns True if the string follows the rules of a title.
  • varName.ljust(value) – Returns a left justified version of the string.
  • varName.rjust() Returns a right justified version of the string.
  • varName.lower() – Converts a string into lower case.
  • varName.upper() – Converts a string into upper case.
  • varName.strip() – Returns a trimmed version of the string.
  • varName.lstrip() – Returns a left trim version of the string.
  • varName.rstrip() Returns a right trim version of the string.
  • varName.partition() – Returns a tuple where the string is parted into three parts.
  • varName.replace(“old”, “new”) – Returns a string where a specified value is replaced with a specified value.
  • varName.rfind() – Searches the string for a specified value and returns the last position of where it was found.
  • varName.rindex() – Searches the string for a specified value and returns the last position of where it was found.
  • varName.rpartition() – Returns a tuple where the string is parted into three parts.
  • varName.rsplit() – Splits the string at the specified separator, and returns a list.
  • varName.split() – Splits the string at the specified separator, and returns a list.
  • varName.splitlines() – Splits the string at line breaks and returns a list.
  • varName.startswith() – Returns true if the string starts with the specified value.
  • varName.swapcase() – Swaps cases, lower case becomes upper case and vice versa.

String Special Characters

There a number of special string characters that have different functions when used inside ” “. Here’re some useful and common ones:

  • \n – Newline – Everything after it goes to next line.
  • \t – Horizontal tab – creates a tab space, similar to when you use ‘tab’ on keyboard.
  • \b – backspace – deletes the character before the it.
  • \r – carriage return – same as \n

In programming Concatenation is a process of appending one string to another. 

\ – escape character is a string character that tell Python that the next character after it should be taken as a string and not as an instruction.

str ( ) is a built-in function that converts and sequence of characters (numbers especially) in to text. 

Mad Libs is a phrasal template word game created by Leonard Stern and Roger Price. It consists of one player prompting others for a list of words to substitute for blanks in a story before reading aloud.

type ( ) is a built-in function (still to cover what built-in functions are later) that determines the Data Type of any data presented. 

input ( ) is a built-in function (still to cover what built-in functions are later) allows a user to insert info into a program/the code. 

print ( ) is a built-in function (still to cover what built-in functions are later) that executes data inside the brackets. The results get printed out on the console/results section.

Integrated Development Environment (IDE) – A digital environment used to develop games, software, hardware, that offers integration from debugging to compiling. Essentially where you write, edit, and run to test your code. 

Variables

More about variables

Info about variables

To book your spot – click here

#WOW – What Outstanding Work – Awards: join us to learn from our students. 

Our top 20 learners are from St Andrews for Girls, Reddam Umhlanga, Evolve Online, Nova Pioneer and Sutherland High

Learners will be presenting their final projects. Come celebrate their successes and lessons learnt with us at our TechWays #WOW Awards.

This event is suitable for learners, parents and their teachers. Book your spot HERE

To book your spot – click here

 

Calling on all high schoolers interested in tech as a career. Join us on Thursday 22 September at 5:30pm.

 

We will be sharing:

  • Some “hot button/in-demand” career pathways – including Automation
  • the skills needed to access these careers
  • some of the job realities in these careers

There are only 100 spaces – so book your spot now – please RSVP here Book

To access the recording – click here

Calling on all high schoolers interested in tech as a career to join us on 16 September at 5:30pm. If you missed it, we’ll host another one on 18 November. 

We covered the following:

  • general tech career tips
  • a few “hot button/in-demand” career pathways and jobs
  • the skills needed to access these careers
  • some of the job realities in these careers

To access the recording – click here

To book your spot – click here

We will be talking to Noelene Kinsley from GC Network. Noelene has been specialised in the exciting career of Genetic Counseling and wants to share her passion for making the world a healthier place using genetics….and data science technology. 

Let’s hear more about the trends in the health/genetics industries, where jobs are moving to and what kind of skills you’ll need in this exciting world of opportunities out there. 

This event is suitable for learners, parents and their teachers. Book your spot HERE

To book your spot – click here

We will be talking to Jason Suttie from Devson. Jason has been in the tech world since he was six years old. He headed IT innovation unit at RMB and has since left to start up his own software consulting company – solving problems and building solutions for clients around the world. 

Let’s hear more about the trends in the software and programming industries, where jobs are moving to and what kind of skills you’ll need in this exciting world of opportunities out there. 

Book your spot HERE

Linux Essentials

Introduces Linux as an operating system, the basic open source concepts and an understanding of the Linux commands. Linux is crucial for cybersecurity.

Comptia
Security+

Gives you the baseline skills you need to secure a company’s systems, software and hardware. This certificate gives practical hands-on skills to pursue a career in cyber security

Certified Ethical Hacker

Will give you skills in Information Security Threats and Attack Vectors, Attack Detection, Attack Prevention, Procedures, Methodologies and more.