PHP part 1: Working with variables


Recommended Posts

Over the last years PHP has become the programming language used for the development of millions of web sites. We see it everywhere: most of the sites we use everyday have files with a .php extension in their URL. Forums, blogs, video sharing sites, file sharing sites, photo albums... even some official company sites. Therefore, the knowledge of PHP is the key to the success for a professional web developer.

In this article we will begin with the basics and most simple aspects of PHP. We will learn how we print a specific text and how we define variables. These are the most simple, yet the most essential functions that everybody must learn, before continuing to the development of content-rich and functionality-rich web applications.

Like every programming language, PHP uses variables to define the values of items. A variable can contain a numerical or alphanumerical values or symbols. Variables in PHP have always a dollar symbol prefix. This sounds familiar to people who already have some knowledge of Perl.

Here is an example where we define the value of a variable and later we print it on the screen using the print() function:

<?php
$text = "Hello world!";
print $text;
?>

which will simply output:

Hello world!

Another example:

<?php
$greeting = "Hello everyone!";
$text = "$greeting How are you?";
print "$greeting $text";
?>

The output result will be:

Hello everyone! How are you?

When a variable is followed by the "=" symbol it means that we define its value for the first time or we redefine it and overwrite any previous value if exists. When it is followed by a ".=" symbol (notice the dot before the equal symbol), it means that the second value is appended to the first one.

Here is an example which explains this better:

<?php

$text = "Good morning. ";
$text = "How are you?";

// Notice that in the second line we used the = symbol
// without a dot

print $text;
?>

The result will be the value that was last defined:

How are you?

Now we will see how the .= symbol works:

<?php
$text = "Good morning. ";
$text .= "How are you?";

// Now we used the .= symbol
// with a dot

print $text;
?>

Good morning. How are you?

Important

When we print a variable using the echo or print command, it is essential that we use the double quotation marks or no quotation marks at all. Single quotation marks will not print the value of the variable, but the name of the variable itself.

An example again, will give more light to this:

<?php
$text = "Hi there";
print "$text";
?>

Result:

Hi there!

which is what we wanted to show.

But if we use single quotation marks:

<?php
$text = "Hi there";
print '$text';
?>

It will print:

$text

In the second part of our PHP articles we will learn specifically about the usage of symbols within variables and when we need to use escape characters to avoid parse errors. We will also learn how we use data passed in a GET query (like file.php?id=1&item=this ) or POST query.

Next parts will be published here on Neowin and also on my blog: Learn PHP Online

Link to comment
https://www.neowin.net/forum/topic/623293-php-part-1-working-with-variables/
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Wow, imagine you dump hundreds of hours into completing things and unlocking stuff and you lose it all. Back in the day when cheats were built into games, you could at least unlock things again that way without spending hundreds of hours again. But those days are long gone for some reason as no one builds cheats into games anymore. So it's even more painful that studio that's on its 6th installment **** it up so badly.
    • Spotify finally removes the disco ball app icon in the latest update by Ivan Jenic Image: Spotify Spotify has just released an update that removes its now infamous disco ball icon. The update reverts the app icon to the familiar flat green logo after weeks of mixed reactions online. The icon arrived on May 13 as part of the company's 20th anniversary celebration and was always intended to be temporary, though Spotify only confirmed that after the backlash started. The disco ball took the internet by storm, as the reception was split. A vocal group of users called it ugly and disorienting, with some iOS users noting that the 3D glowing effect made the app look like it was stuck mid-update. On the other end, the icon picked up a following of its own. Its retro, three-dimensional look immediately stood out against the flat, minimalist aesthetic that has dominated app design for years. It even started a small movement, spawning what people started calling "discomorphism," a mashup of disco and skeuomorphism. Other brands started posting disco ball versions of their own logos, probably in an effort to ride the wave of memes that flooded the internet during late May. Spotify has had a turbulent relationship with its user base lately. Besides the disco ball icon, which certainly wasn't appreciated by everyone, the company has also received backlash for its willingness to include AI-generated music on its platform. On May 17, Spotify promised the old icon would return “in a few weeks.” And now it looks like that time has finally arrived. So, whether you liked the disco ball or it made you uncomfortable, it’s now gone for good. The next time you update the Spotify app on your phone, the old, flat-design icon will return.
    • Playground Games confirms Forza Horizon 6 save wipe bug by Taras Buria Forza Horizon 6 was launched last month to critical acclaim (check out our review here), and it became a smash hit in an instant. Now, weeks into the launch, with die-hard fans clocking hundreds of hours, Forza Horizon 6 is facing a serious issue: save wipes. After multiple complaints on Reddit and social media, the studio issued a statement. The problem with missing saves came shortly after Playground Games promised the initial batch of gameplay tweaks and improvements. Unfortunately, there seems to be no temporary fixes for those affected by unexpected save wipes. However, the studio published a new support document with a few important steps users should try. First, affected gamers should open a support ticket immediately (go here to file one) so that the support team can try recovering the lost progress by reverting to an earlier save. Playground Games says this should be done the same day the issue occurs. Meanwhile, gamers are urged not to start new play sessions or create new saves. The studio also published a few things gamers should try to avoid to prevent potential progress loss: Ensure your Gaming Services app on PC or XBOX Series X|S console is fully up to date. On XBOX Series X|S consoles, disable Quick Resume for Forza Horizon 6: To disable Forza Horizon 6 from using Quick Resume, highlight the game box art anywhere in the console experience (Home, My Games & Apps, Pins, etc) and then press the Menu button, then go to Manage game and add-ons > Quick Resume settings > Disable Quick Resume. Ensure you are online when ‘quitting’ the game. Give your saved time to sync to the cloud before powering off or switching devices. Do not force quit the game during save screens. Do not power off the device during gameplay. Always "Quit" (console) or "Exit to desktop" (PC) once you've finished your play session, ensuring the save icon is not visible when you’re closing the game. Before turning off your console, shutting down your PC, or force-closing the Steam app, give your devices or clients at least a few minutes to ensure your latest progress has been synchronized with the cloud. This will reduce the risk of progress reversions as you switch between different platforms. XBOX Series X|S consoles, Steam, and the XBOX app on PC all include game save indicators that confirm your progress has been synced. You can read more about the bug in the official support document here. Forza Horizon 6 is currently available on PC (Steam and the Microsoft Store), Xbox Series X|S, and Game Pass. The game is also coming to PlayStation 5 later this year.
  • Recent Achievements

    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
    • Week One Done
      highriskpaym earned a badge
      Week One Done
    • Week One Done
      FBSPL earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      518
    2. 2
      PsYcHoKiLLa
      199
    3. 3
      +Edouard
      157
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!