Professional Documents
Culture Documents
String Concatenation
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.
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
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.
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
Instructions
1.
2.
Displaying Text
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
“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:
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:
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!