- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
PHP Basics
In this part of the PHP tutorial, we talk about basic programming in PHP.
All the PHP code is surrounded by two delimiters, <?php
and ?>
.
<?php # PHP code ?>
PHP code is put between two delimiters.
Output
Output from our PHP scripts is sent to the console. Note that we say console because here we use the PHP_CLI
command line interpreter. If we test these examples on the web, the output will be sent to the browser.
printing.php
<?php $a = 23; print $a; ?>
This PHP script assigns a value to a variable. It prints it to the console.
$a = 23;
We assign a value 23 to the $a
variable. Each variable starts with a dollar character. This PHP code line is a statement. Each statement ends with a semicolon. In PHP, semicolons are not optional like in JavaScript or Ruby. They are obligatory.
print $a;
We print the $a variable to the console. The print
keyword does not add a new line to the output. If we want a new line, we must put it manually. The print
keyword takes only one argument.
echoing.php
<?php $a = 23; $b = 24; echo $a, "\n", $b, "\n"; ?>
In this script, we use the echo
keyword. It is similar to the print
keyword. Unlike the print
keyword, it can take multiple arguments.
$a = 23; $b = 24;
We define two variables.
echo $a, "\n", $b, "\n";
We print the variables to the console. We also include the new line characters. Arguments can be separated by commas.
$ php echoing.php 23 24
This is the output of the script.
Command line arguments
PHP scripts can receive command line arguments. They follow the name of the program. The $argv
is an array holding all arguments of a PHP script. The $argc
holds the number of arguments passed, including the name of the PHP script.
arguments.php
<?php echo "There are $argc arguments\n"; for ($i=0; $i < $argc; $i++) { echo $argv[$i] . "\n"; } ?>
This script works with command line arguments.
echo "There are $argc arguments\n";
We print the number of arguments passed to the script.
for ($i=0; $i < $argc; $i++) { echo $argv[$i] . "\n"; }
In the for loop, we go through and print all arguments. Loops are covered later in the tutorial.
$ php arguments.php 1 2 3 There are 4 arguments arguments.php 1 2 3
We pass three arguments to the script. The name of the script is also an argument to the PHP script.
Types
PHP is a weakly typed language. It works with types, but the programmer does not specify them when declaring variables. A data type is a one of various types of data, as double, integer, or boolean. Values of a certain data type are from a specific range of values stating the possible values for that type, the operations that can be done on that type, and the way the values of that type are stored. PHP works implicitly with data types. Programmers do not specify explicitly the data types.
dynamic.php
<?php $a = "Jane"; echo "$a \n"; $a = 12; echo "$a \n"; $a = 56.4; echo "$a \n"; $a = true; echo "$a \n"; ?>
In this PHP script, we have an $a
variable. First, we assign it a string, then an integer, a double, and finally a boolean value. If we assign a string to a variable the PHP automatically creates a string variable.
$ php dynamic.php Jane 12 56.4 1
Running the script we get this output.
gettype.php
<?php $temperature = 12.4; $name = "Jane"; $age = 17; $values = array(1, 2, 3, 4, 5, 6); class Being {}; $somebody = new Being(); echo gettype($temperature), "\n"; echo gettype($name), "\n"; echo gettype($age), "\n"; echo gettype($values), "\n"; echo gettype($somebody), "\n"; ?>
In the above PHP script, we dynamically create five types.
$temperature = 12.4;
A double variable is defined.
$name = "Jane";
A string variable is defined.
$age = 17;
An integer variable is defined.
$values = array(1, 2, 3, 4, 5, 6); class Being {};
This is an array and a class. Both types will be covered later in more detail.
echo gettype($temperature), "\n";
The gettype()
function returns the type of the variable in question.
$ php gettype.php double string integer array object
This script lists the basic types of PHP.
Constants
In PHP, we can create constants. A constant is a name for a value that, unlike a variable, cannot be reassociated with a different value. We use the define()
function to create constants in PHP.
constants.php
<?php define("BLUE", "0000FF"); echo BLUE, "\n"; echo defined("BLUE"); echo "\n"; ?>
In this PHP script, we define a BLUE
constant.
define("BLUE", "0000FF");
Here we define the BLUE
constant. It is a convention to write constants in uppercase letters.
echo BLUE, "\n";
Here we use it. Note that constants are not preceded by the $
dollar character.
echo defined("BLUE");
We have used another function, the defined()
function. It checks if a particular constant exists. It returns true if it does.
$ php constant.php 0000FF 1
Running the example gives the above output.
PHP also has some predefined constants.
predefined_constants.php
<?php echo TRUE; echo "\n"; echo PHP_VERSION; echo "\n"; echo PHP_OS; echo "\n"; echo __LINE__; echo "\n"; echo __FILE__; echo "\n"; echo DIRECTORY_SEPARATOR; echo "\n"; echo PHP_DATADIR; echo "\n"; ?>
Here we print some built-in PHP constants. For example, the PHP_OS
constant prints the OS version on which the PHP was built.
$ php predefined_constants.php 1 5.6.17 Linux 9 /home/janbodnar/prog/php/basics/predefined_constants.php / /usr/local/share/php
On our system, we get this output.
Variable interpolation
Variable interpolation is replacing variables with their values inside string literals. Another names for variable interpolation are: variable substitution or variable expansion.
interpolation.php
<?php $age = 17; echo "Jane is $age years old\n"; ?>
The $age
variable is replaced with the value 17 in the string enclosed by double quotes.
$ php interpolation.php Jane is 17 years old
This is the output.
nointerpolation.php
<?php $age = 17; echo 'Jane is $age years old\n'; ?>
However, this does not work if we use single quotes. In this case, no interpolation happens and no special characters are working.
$ php nointerpolation.php Jane is $age years old\n
We see a verbatim output of the string.
Including files
PHP code is split in multiple files for bigger programs. We use the include
statement to join various PHP files.
common.php
<?php define("VERSION", 1.12); function get_max($x, $y) { if ($x > $y) { return $x; } else { return $y; } } ?>
Let's say, we have a common.php
file, in which we define some constants and functions.
myfile.php
<?php include "common.php"; echo "The version is " . VERSION . "\n"; $a = 5; $b = 3; echo get_max($a, $b), "\n"; ?>
And we have another file which wants to use the aforementioned definitions.
include "common.php";
We simply include the definitions to our file with the include
keyword. We must specify the exact path to the common.php
file. In our simple case, both files are in the same directory.
$ php myfile.php The version is 1.12 5
This is the output.
This chapter covered some basics of the PHP language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论