Decorated wood boxes

Declaring and assigning to variables

As we’ve demonstrated, after “Hello, World”, "Hello {$name}" or f"Hello {name}" might be about the most popular initial port of call in language guides. Makes sense, since varying output according to circumstances lies right at the heart of any project. If you’re familiar with variables in PHP, you likely won’t have any difficulty adapting to the Python equivalent

PHP Code Example

In a minimal change to our Hello, World example, let’s create a variable, assign a string to it, and print the result:

$name = "Bob";
print "Hello, {$name}\n";

Python Code Example:

Aside from syntax variations there is very little different in the Python version.

name = "Bob"
print(f"Hello, {name}")

Note For more about print and f-strings see the Hello, world article.


Put simply, the practical difference here is a matter of a dollar character. In other respects, you’ll find things pretty familiar on a functional level.

Both PHP and Python are dynamically typed languages. That means that that you can assign any type (for example an integer, a string or an object) to a variable. You can change a variable’s type at any time simply by assigning to it once again. In neither PHP nor Python do you need to declare a variable’s type before assigning a value to it (though you can constrain the types that can be assigned to function arguments and class properties in PHP).

In both languages, variable names can be any combination of alphanumeric and underscore characters so long as the first character is not a number. Case matters in both PHP and Python variables. Python reserves some words that you cannot use as variable names. This restriction is not needed in PHP, since the variable syntax makes it possible to distinguish, for example, between $break and break.

The PEP 8 style guide says that Python variables should be named using ‘snake case’. That is, individual words should be distinguished from one another by underscores rather than capital letters. Other sources suggest you’re fine going your own way, but that you should be consistent about whatever convention you choose.

Both PHP and Python make it pretty easy to assign multiple variables in a single statement:

In PHP you’d use the shorthand array syntax:

[$a, $b, $c] = [1, 2, 3];

In Python it’s even easier:

a, b, c = 1, 2, 3

Both languages allow you to chain assignment to apply a single value to multiple variables.

$a = $b = $c = 9;
a = b = c = 9

One gotcha. Although you can chain assignments in Python, that does not mean that you can use assignment as a side effect in conditional statements. The assignment operator in Python does not resolve to the value it assigns as it does in PHP so you can’t play this kind of trick by default:

if ($name = getName()) {
    print "{$name}\n";

However, Python 3.8 provided assignment expressions which use := – the walrus operator. Assignment expressions assign the result of the right hand operand to the left hand operand and resolve to that value. This means you can assign and test at the same time.

if (name := getName()):

See also

Further Reading:

Stay up to date with the Python for PHP Programmers project

Yes! Please sign me up to this newsletter

Photo by Clem Onojeghuo on Unsplash