How can I assign value to a field? - php

How can I assign the value 1990 to born_year in $u0? I get an error massage saying Invalid text string.
<?php if($u0->i_sex=='98') { ?>
$u0->born_year =1990;
<?php } ?>

All PHP inside the " <?php ?>" tag
<?php
if($u0->i_sex=='98') {
$u0->born_year =1990;
}
?>

You are using PHP outside of the PHP tags.
<?php is used at the start of your PHP code.
?> is used at the end of your PHP code.
Try:
<?php
if($u0->i_sex=='98') {
$u0->born_year =1990;
}
?>

Related

how to add echo variable inside other variable from other file?

I am making static php and i want somthing like this (i am a php beginner :):
From this:
<?php $titleid="example title"; ?>
To this:
<?php $title="<?php echo $titleid; ?>"; ?>
To get this:
<h1><?php echo $title; ?></h1>
And then, the expected output is:
<h1>example title</h1>
I use this cause the variable $titleid is in other php file.
In this line
<?php $title="<?php echo $titleid; ?>"; ?>
You should not use the echo, since at that time you don't want any output. Also avoid the double <?php ?> brackets.
Just write that line as
<?php $title=$titleid; ?>
First, you open <?php tag only once in file. if you want get example title in your pages, you define this content one variable and then get result with echo :
Ex
<?php
$title = "<h1>example title</h1>";
echo $title;

PHP variable name concatenation not working

The php variable is not displaying the result.
<h2 class="bold"><? echo ${"application->client->s_MEMBERCLUB_STATUS_{$i}_NAME"}; ?> Member</h2>
You can use
<?php echo $something; ?>
or
<?= $something; ?>
Which is the shorthand.
If you want to use the original code, although it is discouraged, you can check out how to enable it here:
PHP tags

$string is not showing in common.php

Let's say I've got 2 files. 1 is common which loads all the design and stuff and one is index.
What I want to do is set a $ in index like this:
<?
$SubId3 = 'test';
include "../../common.php";
?>
Then in common I want to have something like
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
I cannot seem to get this working. Meaning if I set it up this way. The index will never show "test".
What am i doing wrong here?
I want to do this since only certain files will contain the string $SubId3, to test some things on certain pages and not others (by adding $SubId3 = 'test'; to that particular file)
Note that <?= is short-hand to output something (think of <?= as <?php echo) and not to execute any other sort of logic or code.
However, it is possible to use the ternary operator this way:
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
This is basically equivalent to this:
<?php
if (empty($SubId3)) {
echo 'homepage';
}
else {
echo $SubId3;
}
?>
So the <?= short-hand should only be used to pass one simple variable or a ternary expression to it; everything else should use the common <?php tag.
Here's a test case for Alex (in the comments) because I can run the above code just fine with PHP 5.4.12, but he seems not to be able to.
common.php
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
index.php (visit this file then)
<?php
$SubId3 = 'test'; // <-- Comment this out for the "homepage" output
include 'common.php';
i think this
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
should be
<?php $SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
<?=?> is short for <?php echo?>
This wont work:
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
If you want to print some stuff, you have to use only the variable, in one block and the IF on another.
<?=$SubId3?>
And:
<?php if(empty($SubId3)) { echo 'homepage'; } ?>
Hope this helps...
Try
<?php
/* echo $SubId3; */
if (empty($SubId3)) {
echo 'homepage';
} else {
echo $SubId3;
}
?>
Consider using different style of coding.
In PHP you have generally three variants:
PHP code only
HTML files with just some echoes
Intermixed PHP and HTML
In first you use echo to output every single bit of the HTML.
Second means you include a PHP script at the top of your HTML file and call appropriate functions / insert text into the template. Just so you can edit your HTML separately from your PHP.
Third makes for sometimes unreadable and complex code, but is fast to write.
<?php if($something) {
while($otherthing) { ?>
<B>text=<?=$index ?></B>
<?php }} ?>
Just a food for thought.
I found the answer guys, thanks for all the help.
I needed to set it in the PrintHeader like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
And the index had to look like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
Somebody on skype helped me. thanks anyways guys!

understanding php start & close tags

<?php
function outLala() {
?>
lala
<?php
}
?>
the result of this php file is a blank page instead of a page with text "lala".
if i change the code as
<?php
function outLala() {}
?>
lala
<?php
{}
?>
the result of this php file is a page with text "lala".
does it mean that the php interpreter will parse the code inside the open and the close tag, and if it finds a function(or something else) is not properly terminated, the interpreter will think the content following the close tag is part of the function?
This is valid PHP, but you don't call the function. Add
outLala();
at the bottom.
You must call function outLala. Example:
<?php
function outLala() {
?>
lala
<?php
}
outLala();
?>

Simple PHP syntax - How to get vSlider names to use post-ID

vSlider - in WordPress I'm using the function supplied in the FAQs:
<?php if(function_exists('vslider')){ vslider('vslider_options'); } ?>
And I'm trying to do this. So its knows to get the post-ID as its name. But its not working.
<?php if(function_exists('vslider')){ vslider('<?php the_ID(); ?>'); } ?>
You cannot nest <?php ?> inside an already open <?php ?>. That is unsupported and syntactically invalid. Just call the function in place.
Apparently, the_ID() is one of those Wordpress functions which prints to the output buffer without returning its value. To get the id returned where it can be useful in a function, use get_the_ID() instead.
<?php if(function_exists('vslider')){ vslider(get_the_ID()); } ?>
The syntax issue becomes more obvious when expressed as properly indented code.
<?php
if (function_exists('vslider')){
vslider(get_the_ID());
}
?>
Yes you can do this by removing the php tags
<?php if(function_exists('vslider')){ vslider('<?php the_ID(); ?>'); } ?>
should be
<?php if(function_exists('vslider')){ vslider(the_ID()); } ?>
and your the_ID() function should return a string at the end

Categories