How to remove warning in echoing a string constant in PHP - php

I declared a string constant named LABTITLE. When I tried to echo it in the title tag and run the code, the title of the webpage shows a warning. Is there something wrong with my code?
Our professor specifically instructed to print the Title in this way.
The Title tag:
<title>
<?php
echo constant("LABTITLE");
?>
</title>
The PHP code:
<?php
define("LABTITLE", "Laboratory Activity No. 2");
?>
Then whenever I run, this shows in the Title tab:
Warning : constant(): Couldn't find constant LABTITLE in C:\xampp\htdocs\Lab2\index.php on line 12

Define Your constant first then you can get value. looks like you are using constant value before you define.
<?php
define("LABTITLE", "Laboratory Activity No. 2");
?>
<title>
<?php
echo constant("LABTITLE");
?>
</title>

Instead you could directly use
<title>
<?php
echo LABTITLE;
?>
</title>
given following is included.
<?php
define("LABTITLE", "Laboratory Activity No. 2");
?>

Related

PHP display $variable in HTML before it is defined

I have a webpage called search.php
I want it to have a title/tab tag that looks like this:
<head>
<title><?php echo $number_count; ?> items found - Mysearch </title>
<head>
But the variable $number_count is 0 until later php scripts are called to query the database and display the items one at a time. At the end of the HTML I can easily display <p> <?php echo $number_count; echo "items found" ?> </p> and it works with the correct count.
It must be defined first.
Best (and only in this case) approach to do it is to do calculations first, then generate website output.
EDIT:
You can do it like that:
<?php
$count = 125;
?><!DOCTYPE html>
....
<title>Title count: <?php echo $count ?></title>
....

useing $page_title within a echo

I want this to work, it doesn't currently, so, if its possible, what do I need to change:
<?php echo $page_title_LEADER; ?>
In the config file I have
define('ENGLAND_LEADER', 'Bob Smith:');
define('SPAIN_LEADER', 'Stan Smith:');
which when I use:
<?php echo ENGLAND_LEADER; ?>
works fine as you would expect, what I'm trying to do is use the page title to auto fill the COUNTRY name part of COUNTRY_LEADER, so I don't have to manually change the name of the country each time.
NB I do have the $page_title set in the page
You can use the constant function for this
<?php echo constant($page_title . '_LEADER') ?>

Is it possible to echo a variable which is declared below the echo in PHP?

The below code will not display any output as the variable is declared below the echo as PHP gets executed line by line. Is there any way to search for the variable in the whole page and then execute the code?
<?php
include "header.php";
$title = "Test";
?>
header.php
<html>
<head>
<title><? echo $title ?></title>
</head>
You need to learn how compilers/interpreters works. PHP is interpreted language and The binary that lets you interpret PHP is compiled.
PHP run from top to bottom.
so its like
<?php // start from here
echo "$title"; <-- $title is undefined here
$title = "Test"; <-- now you declared $title with value so it goes in memory now
//end
So you need to first check weather $title is set or not than respond according to it
if(isset($title)){
echo $title;
}
According to your logic, I suggest you to use contants like below:
Create a separate file, let's say constant.php and include it on all other pages
<?
define("TITLE", "This is title");
?>
Use it like below:
<?php echo TITLE;?>
Thanks

How can I access a variable in one block of PHP code in another block of PHP code in the same file?

I have something like this:
PHP code at the start:
<?php
$variable="example";
?>
Then HTML code:
<html>
<head>
<title>Title</title>
</head>
<body>
Then again PHP:
<?php
// code comes here, and I want to access variable $variable here.
?>
And then HTML code ends:
</body>
</html>
Is it possible to do this somehow? I don't want to create another file; I need to do this in this file.
Not Required unless if you are accessing it under functions ( as it will lose their scope)
test1.php
<?php
$var = 1;
//.. your code...
?>
<html>.....
<?php
echo $var; // prints 1
whereas the below code won't work...
<?php
$var = 1;
function displayVar()
{
echo $var; // You will get a notice .. !
}
Just do what you stated above and it will work.
<?php
$variable = 'Hello';
?>
<html>
<head>
</head>
<body>
<?php echo $variable; ?>
</body>
</html>
The above example will display a simple webpage with 'Hello' as the content. This is one of best strength of PHP actually.
try this
echo ($variable);
or
print($variable);
If it is the same file, yes it is possible, unless the variable is in a function. But this is a very simple question, that you could have tested yourself.

php function in html link

I am attempting to call a website root by using a PHP function within a HTML link.
I have created the function bloginfo() below and the correct link output is http://www.example.com/subdirectory/file.php.
The two methods of calling the function are similar but method 1 does not work and method 2 works.
Please can somebody explain why method 1 does not work and suggest a solution. Thanks.
<?php
function bloginfo($show) {
switch ($show) {
case 'template_url':
echo "www.example.co.uk";
break;
}
}
// Method 1 - does not work
echo "test link 1";
?>
<html>
<body>
<!-- Method 2 - works! -->
test link 2
</body>
</html>
Update
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">
Thanks for your help everyone. Unfortunately I could not get the common answer (above line of code) to work as for some reason 'www.example.com' would be printed but not as a link, and the link direction simply became '/subdirectory/file.php'.
To solve the problem I gave up on incorporating a function and decided to simply use the PHP Define method below which works for both methods.
<?php
//this line of code can be put into an external PHP file and called using the PHP Include method.
define("URL", "www.example.com", true);
// Method 1 - works!
echo "test link 1";
?>
<html>
<body>
<!-- Method 2 - works! -->
test link 2
</body>
</html>
The double quoted string is making your php block in the first method just a plain old text string. Try this:
echo "test link 1";
You've nested some php tags
echo "test link 1";
or this:
echo "test link 1";
It was because you were using within tags and not telling it to echo either. The below code will work - I have commented out your old line and added a new line that will work
<?php
function bloginfo($show) {
switch ($show) {
case 'template_url':
echo "www.example.co.uk";
break;
}
}
// Method 1 - does not work
//echo "test link 1";
echo "test link 1"; <--- changed the above line to this... now both this and the lower line work
?>
<html>
<body>
<!-- Method 2 - works! -->
test link 2
</body>
</html>

Categories