You are on page 1of 10

PHP STRINGS AND VARIABLES

String Concatenation

It can be useful to combine two strings together. This process is called string


concatenation, and we can use the concatenation operator ( .) to do this.

An operator is a character that performs a task in our code. The computer will
take the string to the left of the concatenation operator, combine it with the
string to the right, and return the resulting single string. Let’s see an example
of string concatenation:
echo "one" . "two"; // Prints: onetwo
Notice how the string “onetwo” was printed. The computer won’t make any
assumptions for us—it will combine the strings exactly as they are without
adding any spaces or line-breaks. If we want spaces, we’ll have to add any
spaces we want ourselves. Here we added a space to the string "one ":
echo "one " . "two"; // Prints: one two
We can also combine, or chain, our operations to get a final result:
echo "one" . " " . "two" . " " . "three"; // Prints: one two three
The concatenation operator takes two strings (the operands) and produces a
string as a result (the return value). As we delve deeper into PHP, we’ll learn
about other kinds of operators. Most will take one or two operands, but
there’s even one that takes three.

Let’s join some strings together!

Instructions

1.
Use echo to print the string "Code" concatenated to the string "cademy".
Stuck? Get a hint
2.
We want to learn a little more about you. Uncomment the line of code that
starts with echo "\nMy name is:" and concatenate the given string with a string
containing your name. Include a space after the colon without editing the
string we provided.
Stuck? Get a hint
3.
Use echo to print a final portmanteau by concatenating these four
strings "\n", "tur", "duck", and "en". Make sure to include a semicolon after the
statement.

Variables

Let’s say I have a really long string in my program, and I’m going to need to use it
multiple times. Do I have to type the string out every time I need to use it? The answer is
“no”. Variables are a fundamental programming concept designed to address this
concern. With variables, we store values so that we can easily reuse them throughout a
program.

Before we can use variables in our code, we need to declare and assign them.

Declaring a variable is the process of reserving a word, the variable name, which we’ll be
able to refer to in our code. It’s good practice to name the variable in a way that
describes the data it holds.

Assignment is the process of associating that variable name with a specific value so that
everytime we use the variable’s name the computer will grab that value.
Creating Variables

Let’s look at an example of creating a variable:


$my_name = "Aisle Nevertell";
In the code above, we’re actually doing two things with a single statement:
we’re declaring a new variable by giving it the name my_name. We’re
also assigning it the value "Aisle Nevertell". The variable $my_name now holds the
value "Aisle Nevertell".
To declare a variable we use the dollar sign character ( $) followed by our
chosen variable name. The dollar sign is known as a sigil; it’s a character that
allows the computer to see quickly that something is a variable.

To assign it a value we use another operator: the assignment operator ( =)


followed by the value we’re assigning to the variable.

Though it can occasionally be useful to separate these actions, we’ll most


often be declaring and assigning variables at the same time.

In PHP, variables names can contain numbers, letters, and underscores ( _), but
they have to start with either a letter or an underscore. Variable names are
case sensitive, meaning that PHP will treat the
variables $my_example and $My_example as two different variables.

One common convention when naming PHP variables is to use an underscore


between words on variable names with more than one word in their name.
This is known as snake case:
$mood = ":)";
$favorite_food = "Red curry with eggplant";
Let’s create some variables!

Instructions

1.
Create a variable and assign to it a string value. You can give the variable any
valid name you’d like and assign a string containing anything you want. End
the statement with a semicolon.
Stuck? Get a hint
2.
Declare a variable $biography and assign to it a string that starts with a new line
character and contains a sentence or two about you.
Stuck? Get a hint
3.
Create a variable $favorite_food and assign to it the string "\n", "tur", "duck",
and "en" concatenated together.

Attributes

If we want to expand an element’s tag, we can do so using an attribute.


Attributes are content added to the opening tag of an element and can be
used in several different ways, from providing information to changing styling.
Attributes are made up of the following two parts:

 The name of the attribute

 The value of the attribute

One commonly used attribute is the id. We can use the id attribute to specify


different content (such as <div>s) and is really helpful when you use an element
more than once. ids have several different purposes in HTML, but for now,
we’ll focus on how they can help us identify content on our page.

When we add an id to a <div>, we place it in the opening tag:


<div id="intro">
<h1>Introduction</h1>
</div>

Instructions

1.

Add an id attribute with the value "introduction" to the <div> tag that’s below


the The Brown Bear <h1> heading.
Stuck? Get a hint

2.

Add an id attribute with the value "habitat" to the opening <div> tag that has


the Habitat <h2> heading as a child.
3.
Add an id attribute with the value "media" to the opening <div> tag that has
the Media <h2> heading as a child.

Displaying Text

If you want to display text in HTML, you can use a paragraph or span:

 Paragraphs (<p>) contain a block of plain text.


 <span> contains short pieces of text or other HTML. They are used to
separate small pieces of content that are on the same line as other
content.

Take a look at each of these elements in action below:


<div>
<h1>Technology</h1>
</div>
<div>
<p><span>Self-driving cars</span> are anticipated to replace up to 2 million jobs
over the next two decades.</p>
</div>
In the example above, there are two different <div>. The second <div> contains
a <p> with <span>Self-driving cars</span>. This <span> element separates “Self-
driving cars” from the rest of the text in the paragraph.

It’s best to use a <span> element when you want to target a specific piece of
content that is inline, or on the same line as other text. If you want to divide
your content into blocks, it’s better to use a <div>.

Instructions

1.
Below the <h2> element that says About Brown Bears, add <p> opening and closing
tags, and inside of the tags put the following text:

“The brown bear (Ursus arctos) is native to parts of northern Eurasia and North
America. Its conservation status is currently Least Concern. There are many
subspecies within the brown bear species, including the Atlas bear and the
Himalayan brown bear.”
Remember to always add two spaces of indentation when you nest elements
inside of <div>s for better readability.
Stuck? Get a hint
2.
Below the <h3> element that says Features, add a paragraph with the following
text:

“Brown bears are not always completely brown. Some can be reddish or
yellowish. They have very large, curved claws and huge paws. Male brown bears
are often 30% larger than female brown bears. They can range from 5 feet to 9
feet from head to toe.”
Stuck? Get a hint
3.
Under the <h3> element that says:
Countries with Small Brown Bear Populations

Add a paragraph with the following text:

“Some countries with smaller brown bear populations include Armenia, Belarus,
Bulgaria, China, Finland, France, Greece, India, Japan, Nepal, Poland, Romania,
Slovenia, Turkmenistan, and Uzbekistan.”

Styling Text
You can also style text using HTML tags. The <em> tag emphasizes text, while
the <strong> tag highlights important text.

Later, when you begin to style websites, you will decide how you want
browsers to display content within <em> and <strong> tags. Browsers, however,
have built-in style sheets that will generally style these tags in the following
ways:

 The <em> tag will generally render as italic emphasis.


 The <strong> will generally render as bold emphasis.

Take a look at each style in action:


<p><strong>The Nile River</strong> is the <em>longest</em> river in the world,
measuring over 6,850 kilometers long (approximately 4,260 miles).</p>
In this example, the <strong> and <em> tags are used to emphasize the text to
produce the following:

The Nile River is the longest river in the world, measuring over 6,850


kilometers long (approximately 4,260 miles).

As we can see, “The Nile River” is bolded and “longest” is in italics.

Instructions

1.
In the first paragraph that starts “The brown bear…”, emphasize Ursus
arctos using the <em> tag.
2.
In the paragraph under About Brown Bears, make the words Least Concern strong
using the <strong> tag.
Line Breaks

The spacing between code in an HTML file doesn’t affect the positioning of elements in the
browser. If you are interested in modifying the spacing in the browser, you can use HTML’s line
break element: <br>.

The line break element is unique because it is only composed of a starting tag. You can use it
anywhere within your HTML code and a line break will be shown in the browser.

<p>The Nile River is the longest river <br> in the world, measuring over 6,850 <br>
kilometers long (approximately 4,260 <br> miles).</p>
The code in the example above will result in an output that looks like the following:

The Nile River is the longest river


in the world, measuring over 6,850
kilometers long (approximately 4,260
miles).

Instructions

1.

Add two line breaks (<br>) after the sentence that ends with Least Concern.

Concept Review

Want to quickly review some of the concepts you’ve been learning? Take a look at this
material's cheatsheet!

You might also like